Useful .htaccess techniques

27 January 2010| 5 Comments| Print

I wrote a post mid last year about protecting your site images from being hotlinked as I found a site that had word for word scrapped one of my blog posts, you can see the post here, and I really wanted to go back to this and run over a few more things the .htaccess file is useful for. I mainly use the .htaccess file to rewrite URIs for example http://mysite.com/site/pages/contact.php would be accessed by visiting http://mysite.com/contact. So the URI is being re-routed to my contact.php page.

Using Mod_rewrite for friendly URLs

You need to make sure if you want to rewrite URI’s that you have the mod_rewrite PHP module installed with your host. Most hosts will have this with the latest stable versions of PHP.

 RewriteEngine On

 RewriteRule ^user/([a-z0-9]+)/$ /user.php?u=$1 [NC,L]
 RewriteRule ^analytics/$ /pages/analytics.php [NC,L]
 RewriteRule ^settings/$ /pages/settings.php [NC,L]

Redirect a domain

If you’ve just moved your site to a new domain and you want to transfer any visitors that visit your old site straight through to your new site just add this to your .htaccess file on your old domain.

RewriteEngine On
RewriteRule ^(.*)$ http://www.yournewdomain.com/$1 [R=301,L]

Restrict certain file type execution

If you have a server that only has php files on, you can lock it down so other file types can’t be executed

Options -ExecCGI
AddHandler cgi-script .js .cgi .asp .jsp. htm .exe .sh

Set custom error pages

Probably the most common use for the .htaccess file is to setup custom error pages. this means that if you’re site throws a 404 not found error the user will see the page at http://yoursite.com/404.php

ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.php

Remove the www in your url

This snippet of rewrite code will remove the www from your site domain url. For example wordpress automatically does this if you visit http://www.papermashup.com you’ll see that the url changes to http://papermashup.com.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Things to note when editing the .htaccess file

  • It’s important to make sure that you don’t have any spelling mistakes or typos in your .htaccess file. It’s very unforgiving and can take your complete site offline throwing a 500 internal server error in some instances. Remember it’s also case sensitive!
  • If you’re going to add comments to your .htaccess file simply put a # in front of the text you want to comment out. This is different from say JavaScript or PHP where you can just add a double forward slash //.
  • Always make a backup of your .htaccess file before making any changes, this way you can easily isolate any problems if your site does down or you experience unexpected results, and always make sure that if you change the .htaccess file you immediately check your site for any problems.

Share:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • DZone
  • Reddit
  • Netvibes

5 Comments

  • Derleth

    oye chato eso estuvo muy bueno principalmente lo ultimo, es muy util :)
    buen post

  • enatom

    bookmarked this.

  • Mike

    I’m having trouble creating the friendly URL’s… I’m able to go to say, http://mikeutter.com/about/ (about.php), but it’s not pulling my CSS or any styling for that matter… thoughts?

    Thanks!

  • ryan

    Mike you can and paste this code to your htaccess file..

    #RewriteRule ^(.*)$ index.php?articles=$1
    #RewriteRule ^(.*)/$ index.php?articles=$1

    feel free to edit the code…..

    thanks

  • Textile Exporter

    I saw your article its pretty good and informative and I am also using this technique to make .htaccess file for my site.

Leave a comment...

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled site. To get your own globally-recognized-avatar, register at Gravatar.

Your Ad Here