5 htaccess Tricks Every Webmaster Should Know
If you’re new to htaccess, here’s a quick introduction. Otherwise, here are 5 sets of htaccess directives every webmaster should know:
1 - Redirect Visitors While You Update Your Site
Update and test your site while visitors are redirected to the page of your choice:
order deny,allow deny from all allow from 123.123.123.123 ErrorDocument 403 /page.html <Files page.html> allow from all </Files>
Replace 123.123.123.123 with your IP address. Also replace page.html with the name of the page you want visitors to see.
2 - Display a Custom 404 Error Page
Your server displays a “404 File Not Found” error page whenever a visitor tries to access a page on your site that doesn’t exist.
You can replace the server’s default error page with one of your own that explains the error in plain language and links visitors to your home page. Here’s how to use your own page:
ErrorDocument 404 /404.html
Replace 404.html with the name of the page you want visitors to see.
3 - Handle Moved or Renamed Pages
You’ve moved or renamed a page on your site and you want visitors automatically sent to the new page when they try to access the old one. Use a 301 redirect:
Redirect 301 /old.html http://yoursite.com/new.html
Using a 301 redirect also ensures the page doesn’t lose its search engine ranking.
4 - Prevent Directory Browsing
When there’s no index page in a directory, visitors can look and see what’s inside. Some servers are configured to prevent directory browsing like this. If yours isn’t, here’s how to set it up:
Options All -Indexes
5 - Create User Friendly URLs
Which of the two URLs below looks friendlier?
http://yoursite.com/about http://yoursite.com/pages/about.html
When it comes to URLs, as long as the meaning is clear, shorter is always better.
With htaccess and an Apache module called mod_rewrite, you can set up URLs however you want. Your server can show the contents of “/pages/about.html” whenever anyone visits “http://yoursite.com/about”. Here are a few examples:
RewriteEngine on RewriteRule ^about/$ /pages/about.html [L] RewriteRule ^features/$ /features.php [L] RewriteRule ^buy/$ /buy.html [L] RewriteRule ^contact/$ /pages/contact.htm [L]
There’s a lot more to mod_rewrite and htaccess. Check out the links below for more details and tricks.
Additional Resources
Apache htaccess Ultimate Guide
Comprehensive guide to .htaccess
mod_rewrite, a beginner’s guide (with examples)
Stupid htaccess Tricks
July 10, 2007
5 tips su htaccess che ogni Webmaster dovrebbe conoscere
Questo post è una traduzione non ufficiale e con aggiunte personali di “5 htaccess Tricks Every Webmaster Should Know”.
Redirezionare i visitatori durante l’aggiornamento del proprio sito
Tutti i visitatori tranne voi (ossia l’IP 123.123.123.123) …
July 10, 2007
htaccess is one of the smaller things that I’ve always overlooked, but I see many things you’ve listed here that make it an invaluable tool to understand.
Thanks a bunch.
July 10, 2007
Short and sweet, very helpful article for people thanks! Be sure to add the Ultimate htaccess guide to the Additional Resources. http://www.askapache.com/htaccess/apache-htaccess.html
July 10, 2007
Wish I had this list a few months ago..
Thanks for the compilation…
July 10, 2007
Thanks everyone - Good to hear the article helps.
@AskApache.com: Great resource! I’ve added it to the list.
July 10, 2007
Great post, very useful.
You can also use something like
RewriteRule ^(.*).html$ http://www.site.com/$1 [R=302,L]
to manage a whole class of obsolete pages to new addresses
July 11, 2007
Pretty good
July 11, 2007
Awesome thanks for the info
July 12, 2007
Thanks for the easy and quick tips. These simple tips can get your visitors quickly to pages, rather than 4040 error pages, that you can direct them to your best resources, too.
July 12, 2007
I’d add that you should block viewing of the .htaccess file:
<Files .htaccess>
order allow,deny
allow from all
</Files>
July 12, 2007
Great tips! thanx!
July 15, 2007
Hey thanks for the mention! These are some great tips indeed! The page redirection (#5) is one of my favorites
I also noticed an article very similar to this one at http://seoinformant.com/?p=3
..not sure if you have anything to do with that, but figured it is something you may want to check out.
July 15, 2007
@m0n: You’re welcome– Your list is amazing, had to include it!
Also, thanks for the heads up. Just got started and someone’s stolen a post already. I’ve asked them to remove it. We’ll see what happens…
July 17, 2007
This is a nice, concise list of things every developer should know. For other things, you have listed some great resources.
Nice work.
July 18, 2007
Muy bueno. Sencillo y claro. Gracias
July 18, 2007
“Using a 301 redirect also ensures the page doesn’t lose its search engine ranking.”
There is absolutely no guarantee that this is true. There is a lot of evidence to suggest that over-use of 301s will actually get your rank *bombed*.
So be really careful!
July 19, 2007
Everyone, thanks for your feedback and the additional tips.
@unsure: I think SEO experts would disagree. 301 redirects aren’t penalized.
July 19, 2007
Ya.. htaccess rocks, but be careful when using it on very active servers (tooooo many visits), it can crash your server :S
)
(SMBE
July 20, 2007
Thank you!

#1 is something I had always meant to work out, but had never gotten around to
Bookmarked
July 20, 2007
wow along with others in these comments I have also always over looked these simple but important tips.
Thanks
July 30, 2007
Since your page validates, I’ll be nice and take the time for a comment: this is something I’ve put off long enough, so thanks for the kick!
August 16, 2007
Simple but very useful tutorial, thanks for sharing the knowledge.
September 7, 2007
Any idea how to use this for
converting dynamic pages to static URL
thanks
January 7, 2008
Useful introduction - it’s certainly got me interested in htaccess and learning more about it. Thanks.
February 14, 2008
Thank you for your work.
February 20, 2008
thanks. ive been looking for something like this.
March 7, 2008
How would you use .htaccess to only redirect or use modrewrite for robots only?
Google adsense is now bypassing and blocking sites that are using robots.txt and I tried the meta tags robots noindex, nofollow. So the only thing left is to use the .htaccess.
April 21, 2008
I like the custom 404 page. Especially useful if you’ve moved or changed your website format and you have old page urls floating out in cyberspace.
April 28, 2008
How can i access my whole site pages through .html even if these all are in .php…..so how can i do with .htaccess file……..
April 28, 2008
[…] Password Robot blog, han publicado un interesante artículo con 5 ejemplos .htaccess sencillos y muy útiles, cómo […]