Beat Your Website into Submission with .htaccess
February 6th, 2008 in Design Tips & Tutorials
by: Matthew Griffin
I devote most of my day to designing and coding. I don't have a lot of extra time to mess with server configuration. But my neglect of that aspect of web design has cost me countless hours of unnecessary development and a near break-down on more than one occasion. My server was bending me to its will instead of the other way around. Somewhere along the way I discovered its weakness—.htaccess. That's the little file that puts password protection on directories (that's all I knew it did). Now I'm beating my websites into submission one command at a time.
If you've never used .htaccess before, all you have to do is create a new empty file called .htaccess. If you're using a WYSIWYG editor like Dreamweaver, you'll want to associate your editor with the .htaccess extension so that you can easily open the file in the editor. Once you've created the file and put some commands into it, you can FTP it into any directory on your web server. All the files in and below the directory containing your .htaccess file will follow its commands. Now let's take a look at some tricks your puppy will do with its new shock-collar on.
IMPORTANT: Some shared hosting providers don't allow .htaccess. It stinks when you find this out after the fact. So before you choose a host, make sure they allow .htaccess.
Forcing PHP to Include Files From a Specific Directory
You can make your PHP include statements start looking in a particular directory instead of a relative directory. This is very helpful if you have a library file that needs to appear on every page of your website. For example, you may want to put your contact information on every page of a website. You would create a directory on your server called "library" and put the HTML file with your contact info in it (contact.htm) into the library directory. Then you would use .htaccess to tell PHP to always look in that directory for includes. The .htaccess file would look something like this:
Remember to use the absolute path to the library directory when you use this command. Once you're done, upload your new .htaccess file into the root directory of your website. Now every time you use this command in PHP, no matter what directory the calling file is in, it will pull your contact file:
Forcing the Server to Execute Directories Like PHP Files
Search engine robots don't like arguments in urls. If you have a link to "article.php?article=55" and another link to "article.php?article=76", some search engine robots will see these as double links and won't index them. But what if you could make a link that looks like "article/76/" accomplish the same thing—that would solve the problem. Here's the .htaccess command to make it happen.
DirectoryIndex index index.php index.html
There are are several other commands that perform this same function but they require more advanced configuration so I listed the example most likely to work on any server. Here's some PHP to extract the "76" as a variable:
Keeping the Public Out of a Directory
Sometimes you will have a directory in the public folder of your website that you don't want anyone to access from the web. If you have a hosting plan that uses Cpanel, you can use directory protection to put a password on the directory. But what if you don't even want password access? Just put the following command into your .htaccess file and upload it into the directory.
You can also use this command to deny specific IP addresses as well. For example:
deny from 126.96.36.199
allow from all
or to allow only specific IP addresses:
deny from all
allow from 188.8.131.52
Custom Error Pages
.htaccess also gives you the power to create your own error pages to replace the ugly default ones—It's easy:
ErrorDocument 403 /err/403.php
ErrorDocument 404 /err/404.php
Executing CSS Files as PHP Files
This is a fun one. Have you ever wished you could put a little PHP into your stylesheet? You can with .htaccess. Just make sure you have phpsuexec compiled with PHP or this one won't work. Here's the .htaccess code:
There hundreds of hacks you can perform with .htaccess—most of them are more complicated and involved than the ones on this list. A quick Google search will reveal a long list of great tutorials if you want to go to the next level. But for now, I hope this article has inspired you to start ruling your host server the way it was meant to be ruled.
- 39 Comments
- 8355 Views