Messing with .htaccess is no fun!

A friend of mine was posting to our common Yahoo! Group, asking for assistance with his WordPress blog. What he was facing was that his permalinks weren’t working. They all ended up pointing to the 404 error file.

What was the error? I think he messed up with his .htaccess file! Its really easy to accidentally remove or misplace this file, since it doesn’t look like a conventional file. You might even think it is a virus or something of this sort!

The .htaccess file is a text file that resides on Apache servers to do lots of configuration stuff! Some common ones are like 301 redirects, solving the canonical domain name issue and rewriting domain names.

Here’s the official description from the Apache tutorial:

.htaccess files (or “distributed configuration files”) provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

Anyway, the point is, if you are good at working on all these stuff, and the Apache Documentation is like bedtime storybooks for you – then it can be a very very powerful tool! Especially the mod_rewrite module.

But… if you are like me and need 10 years to figure out how regular expressions work, my advice would be to stay clear of the .htaccess file! Heh. Well, its not that bad actually… but yeah, it can cause your whole site to crumble!

WordPress Permalink Settings

So what happened for my dear friend? When WordPress is configured for permalinks with search engine friendly URLs, (i.e. URLs without a question mark), it needs to add a couple of lines to your .htaccess. When you mess that up. Wallah, its like magic… all your pages are gone! :mrgreen:

But how did I find out? I used the default URL (i.e. using http://www.theurl.com/?p=123 method) to see if his posts are still there!

Here is what WordPress adds, when you select a different permalink structure.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

It basically says… “if the mod_rewrite engine is available, and the requested URL is not an existing file or directory, then channel that request to index.php.” *Hey, I took a day to figure that out!* :mrgreen:

I suppose something is done in the WordPress system to convert all the nice URLs to query the database for your nice posts.

Anyway… so what should he do? Simple. Just make sure the .htaccess file is available and writeable (you can just upload an empty text file named .htaccess and chmod it to 666), and go to your WordPress admin and re-apply your permalink settings.

Then you are done. All your posts will be back up, safe and sound. *phew*

Comments are closed.