Drilling deep into .htaccess and WordPress

in WordPress Tutorials

The Doolittle Drill

.htaccess is a configuration file for use by your Apache Web Server. .htaccess files are placed in directories and when Apache serves documents from those directories it looks to the .htaccess file for special instructions. What does this file do? It alters the configuration of the Apache Web Server software to either enable or disable functionality and features that are installed with the Apache system. The facilities include basic redirection, password protection, image hot link prevention and url encoding. Redirection is useful if a 404 (page not found) error comes up. Instead of an ugly 404 page you can redirect to something a lot more interesting. My favorite 404 page is over at GitHub. If you are not going to get served the page you were looking for at least you should be able to get something entertaining!

URL encoding is useful for what is known as URL canonization. For example, in the old days you always had to put the server name as the first part of the URL. http://wpmods.com would not be recognized unless you put http://blog.wphub.com – the www indicating the name of the server. “www” is just the traditional name, of course, for a web server. It could be given any legal server name like http://chewbacca.wpmods.com. If you don’t have to type www, that is because the name has been canonized. Another way it is canonized is the fact that you don’t have to end the url with index.php.

I’ve added the following line to my .htaccess file:

AddType x-mapp-php5 .php

What this does is it tells Apache to use PHP version 5 for all .php files. My hosting provider has version 5 and 4 and defaults to 4 if not otherwise specified. This has been very helpful to me. I tend to keep up with my plugins and use the latest so this makes a lot of sense. PHP 6 currently has no estimated time of arrival, but when it is available I will look into the sense in eventually updating my directive to use that revision level instead. Currently, enough things don’t work with PHP 4 that I don’t bother with it anymore. Nothing I have chosen to use breaks with PHP 5. Always read the descriptions of the themes and plugins you are using to see what level of PHP they require and are compatible with. In a pinch you can always drop a .htaccess file with different directives into each sub-directory. Just be aware that if the PHP files have dependencies on other files and those are in directories with different directives you will have a clash. That was the main reason I specified PHP 5 everywhere instead of letting 4 be the default and 5 set only where needed.

You can learn more about .htaccess at the htaccess-guide website.

Here are some quick hacks:

Want to tunnel your page via https for security reasons?

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Prevent directory listing of a directory:

Options All -Indexes

Re-orginzied your site and some things have moved? redirect old links:

Redirect 301 /original/filename.php http://domain.com/updated/filename.php

Hopefully you found these quick hacks useful!

By the way the image is a “Space tracked tractor drill transport utility vehicle mining”, (c) Brickshelf LLC. Hippest drill image on the web!