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:
$article_id= $url_explode[count($url_explode)-1];
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 66.148.232.105
allow from all
or to allow only specific IP addresses:
deny from all
allow from 66.148.232.105
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:
SetHandler application/x-httpd-php
</FilesMatch>
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
- 8588 Views
Comments
Posted By: hari on 02/06/08
The best part of .htaccess is the ability to rewrite URLs and it's quite intricate and complex. But even with basic URL rewriting you can do quite a lot. :)
Posted By: on 02/06/08
Thanks, Hari.
Posted By: g1smd on 02/06/08
*** Forcing the Server to Execute Directories and HTML Files Like PHP Files *** This section needs to be split into two parts, as well as editing to add information about the AddType directive. AddType application/x-httpd-php .html The example code you gave was for something else... edit that and add niformation about what the DirectoryIndex command really does. :-)
Posted By: g1smd on 02/06/08
<p>*** Forcing the Server to Execute Directories and HTML Files Like PHP Files ***</p> <p>This section needs to be split into two parts, as well as editing to add information about the AddType directive.</p> <p>AddType application/x-httpd-php .html</p> <p>The example code you gave was for something else... edit that and add niformation about what the DirectoryIndex command really does.</p> <p>:-)</p>
Posted By: g1smd on 02/06/08
[p]*** Forcing the Server to Execute Directories and HTML Files Like PHP Files ***[/p] [p]This section needs to be split into two parts, as well as editing to add information about the AddType directive.[/p] [p]AddType application/x-httpd-php .html[/p] [p]The example code you gave was for something else... edit that and add niformation about what the DirectoryIndex command really does.[/p] [p]:-)[/p]
Posted By: g1smd on 02/06/08
Ahh frik. I give up.
Posted By: RT Cunningham on 02/07/08
My host requires php directives be placed in an .ini file separate from .htaccess and it's actually just as simple.
Posted By: on 02/07/08
Thanks for the info, g1smd. As I mentioned, there are more suitable commands to accomplish this task but I've had trouble getting them to work on certain shared hosting plans. The DirectoryIndex command just gets it done.
Posted By: on 02/07/08
RT Cunningham, I've dealt with a few hosts that work the same way. Thanks for mentioning that.
Posted By: Graham Smith on 02/07/08
Came across your article on DIGG, so have DIGGED it. Great article, so one more to add to the to-do-list. Great stuff Graham
Posted By: g1smd on 02/08/08
But the DirectoryIndex directive is not used for Forcing the Server to Execute HTML Files Like PHP Files. That isn't what it does at all; and that was my point.
Posted By: Matthew Griffin on 02/08/08
You're right, g1smd. I accidentally left that in there. I'll take it out.
Posted By: Erica DeWolf on 02/08/08
Wow, this is all real great information and a lot to take in. I'm not going to lie, a lot of it went over my head (I'm more of a creative than a techie, but I try), but I'm going to try to understand more of it...Thanks!
Posted By: Matthew Griffin on 02/08/08
Thanks, Erica. Web design is such a strange mixture of disciplines. It's hard to be everything to everyone sometimes.
Posted By: g1smd on 02/08/08
Don't take it out. Slap a heading in front of it and add a few words to make it another section. That was my suggestion above.
Posted By: E. I. Sanchez on 02/09/08
I moved my blog directory from /blog to /main I have a 301 redirect in my .htaccess How long should I keep that 301 redirect? Is there a best practice?
Posted By: on 02/09/08
I don't know of a best practice for your situation, Edgar. But considering all the possible links pointing to your old directory (bookmarks, social media sites, possibly your RSS feed), I would leave it up there for a long time. You may see your old articles drop rank on Google as a penalty for the 301 redirect, but that's unavoidable. Just make sure that all the links on your site are pointing to the new directory.
Posted By: g1smd on 02/09/08
The 301 is a "permanent redirect". That means three things. The content is now at a new location. It is never coming back to the old location. The redirect might need to be in place forever (unless the old URL gets used for some new purpose at a later date).
Posted By: Webhosting Reality on 02/10/08
You can also use .htaccess when you want to redirect a url to another url. For instance, you can force a nonwww url to go to a www url. I'll leave that to you for your next tip on .htaccess.
Posted By: MikesMultiMedia on 02/17/08
Wondering how to have a *.php file execute imbedded server side includes. I know there is a php version to do a server side include like function (like recurring footers, etc) I'm just more familiar with ssi's, and would just like to modify the *.php file and include my already existing ssi's into it - I've tried telling the server, using .htacces to parse .php files, but if it does that, it forgets to also process the file for its php code, which gets ignored and ends up as text, or what not. Any solutions?
Posted By: on 02/18/08
Mike, if I'm understanding you correctly, you are looking for the php command include and include_once. Go to PHP.net and look those up. That should do the trick.
Posted By: Pearcy on 11/02/08
Does anyone happen to know, <br> If I move a site to a new server, do you happen to know if each link needs to have it's own rewrite rule or can I use a global command like: <br> rewritecond %{http_host} ^portlandurbanpages.com [nc] rewriterule ^(.*)$ http://www.portlandurbanpages.com/ [r=301,nc] <br> Thanks, Pearce
Posted By: Matthew Grffin on 11/03/08
Pearcy, I have another .htaccess article that will answer your question. Just do a search up at the top or go to the Programming and It category.
Posted By: George on 12/15/08
You're right, g1smd. I accidentally left that in there. I'll take it out.
Posted By: Vimax on 06/27/09
There are a lot of solutions, pills and devices for <a href="http://www.penisenlargement4us.com">penis enlargement</a>, special exercises, and surgery, but the enlargement pills are far the best solution, especially the ones made by natural ingredients. <br> One of the best solutions on the enlargement pills market today is <a href="http://buyvimaxnow.com">Vimax</a> pills, from Pills Expert.