Useful .htaccess Tricks for Webmasters

I just did a little google search and found a lot of .htaccess tricks that you might want to use for your websites. Those who are new to .htaccess Password Robot Blog has a good quick introduction. There’s also a online .htaccess editor that can be useful available in English, French, Spanish, Chinese and more.

Don’t forget that if you or the person you know is working on the server has a root access to the server you could do the same thing without using .htaccess, which is recommended.

Custom 404 Error Pages

Whenever your visitors access to the page on your site that doesn’t exist, they get an error saying “Page Cannot be Found” which is not user-friendly. Here’s a sample on how to use your custom page:

1
ErrorDocument 404 /404.html

You could make it like this too:

1
ErrorDocument 404 http://www.ragard-jp.com/en/404.html

Need some ideas on how to design a 404 error pages? Smashing Magazine has some smashing 404 error pages listed.

Redirect Visitors

Sometimes you don’t want your visitors to see you working on the website. Instead you would like to show them a page telling that you are updating or still under construction. Here’s the code that can let you do that:

1
2
3
4
5
6
7
8
9
order deny,allow
deny from all
allow from 123.456.789.101
 
ErrorDocument 403 /update.html
 
<Files update.html>
allow from all
</Files>

Make sure to change 123.456.789.101 to your IP address and change update.html to the file name you would like to display to your visitors.

Change Home Directory

Usually when visitors types in the url to your site, by default they will land on index.html, index.php, or index.htm. You could actually change the file you would like to display:

1
DirectoryIndex new.html

Change new.html to any file you would like to have as your home directory.

Prevent Directory Browsing

When you don’t have any index pages in your directory, visitors can easily see what you have inside. To prevent this you could simply make a blank index.html on every single directory or just set up using .htaccess:

1
Options All -Indexes

Protecting Data Files

If your using a script that saves data or log you need to make sure that it is secured. If you have information about your customers, people can download the data or log file by just accessing from the browser. You can protect it easily:

1
2
3
<Files ~ "\.(dat|log|csv)$">
deny from all
</Files>

Not to worry, your cgi or php can access the data files like before.

Force To Download Files

Have you ever had a problem visitors streaming your media files instead of downloading? With this code you can force your visitors to download even when they left click. Just try it and you will see how sweet this is:

1
2
3
4
5
6
AddType application/octet-stream .avi 
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .mp3
AddType application/octet-stream .wma
AddType application/octet-stream .pdf

It even works for pdf files!

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google
  • StumbleUpon
  • Facebook
  • Furl
  • Mixx
  • Technorati
  • Blue Dot
  • Design Float
  • Live
  • Ma.gnolia
  • Reddit
  • TwitThis

Tags:

Comments

  1. celeb
    September 15th, 2008

    extremely useful… you could just add this:

    RewriteEngine On
    RewriteRule ^(.*)\/article\/(.*)\/ %{DOCUMENT_ROOT}/article.php?lang=$1&name=$2 [L]

    for making “nice” URLs

  2. ne-design
    September 15th, 2008

    Thanks for another good trick celeb!

    I will be making a part 2 for this one. There’s too many .htaccess tricks I couldn’t write it in one day.

  3. Super Useful .htaccess Tips | reallygoodmagazine.com
    September 15th, 2008

    [...] These are awesome.  For those of you who have been setting up sites for some time, you have probably needed these at some point.  If you haven’t you can sure use them.  HERE IT IS [...]

  4. celeb
    September 16th, 2008

    maybe u could just make cheat-sheet you know? might be really useful… with all of the tricks together

  5. AskApache
    September 20th, 2008

    Ya isn’t .htaccess awesome! Those particular tips are from versions of my original .htaccess tutorial which has since been updated to the uultimate htaccess tutorial, a series of .htaccess cheatsheets you are free to use!

    :)

    Also see this passwordrobot post: 5 htaccess tricks every webmaster should know.

  6. ne-design
    September 20th, 2008

    Thanks for your comment AskApache!
    I always love your tutorials on Apache.

  7. Laszlo Baranyai
    September 25th, 2008

    If you have a download script, you can prevent direct access to directory content by (similar to your redirect code):

    order deny,allow
    deny from all
    allow from localhost

    I would extend the tricks with some anti-spam options line:

    SetEnvIfNoCase Referer “.*(keyword1|keyword2|etc).*” BadReferrer
    order deny,allow
    deny from env=BadReferrer

    and:

    order allow,deny
    deny from 1.2.3.4
    deny from 2.3.4.
    deny from 3.4.
    allow from all

    This latter with B and C zones will punish the spammers’ service providers and other users as well. Use carefully!

Leave A Comment