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:
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.
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.
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.
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
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
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) …
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.
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
Wish I had this list a few months ago..
Thanks for the compilation…
Thanks everyone - Good to hear the article helps.
@AskApache.com: Great resource! I’ve added it to the list.
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 :-)
Pretty good :)
Awesome thanks for the info :-)
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.
I’d add that you should block viewing of the .htaccess file:
<Files .htaccess>
order allow,deny
allow from all
</Files>
Great tips! thanx!
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.
@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…
This is a nice, concise list of things every developer should know. For other things, you have listed some great resources.
Nice work.
Muy bueno. Sencillo y claro. Gracias
“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!
Everyone, thanks for your feedback and the additional tips.
@unsure: I think SEO experts would disagree. 301 redirects aren’t penalized.
Ya.. htaccess rocks, but be careful when using it on very active servers (tooooo many visits), it can crash your server :S
(SMBE :P )
Thank you!
#1 is something I had always meant to work out, but had never gotten around to :)
Bookmarked :)
wow along with others in these comments I have also always over looked these simple but important tips.
Thanks :P
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!
Simple but very useful tutorial, thanks for sharing the knowledge.
Any idea how to use this for
converting dynamic pages to static URL
thanks
Useful introduction - it’s certainly got me interested in htaccess and learning more about it. Thanks.
[...] should at least check out [...]
Thank you for your work.
thanks. ive been looking for something like this.
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.
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.
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……..
[...] Password Robot blog, han publicado un interesante artículo con 5 ejemplos .htaccess sencillos y muy útiles, cómo [...]
[...] from the article: 5 htaccess Tricks Every Webmaster Should Know June 2, 2008 — wordpress htaccess from the article: 5 htaccess Tricks Every Webmaster Should Know [...]
[...] 5 htaccess Tricks Every Webmaster Should Know [...]
[...] redirects mod_rewrite, 301/401 Redirects & Optimizing Apache Canocial domain 301 redirects 5 htaccess Tricks Every Webmaster Should Know Apache Redirect [...]
awesome I didn’t know you could do all that with a .htaccess file… I just know about doing redirects.
[...] are five sets of htaccess directives every webmaster should [...]
Simple and sweet.Easy to understand .I got what i wnat .I have read 100 of articals but this one is best one of them.
Very informative blog post for a trainee in a SEO company. It helped me a lot in understanding .htaccess and it’s use. Keep it up.
[...] redirects mod_rewrite, 301/401 Redirects & Optimizing Apache Canocial domain 301 redirects 5 htaccess Tricks Every Webmaster Should Know Apache Redirect [...]
“1 - Redirect Visitors While You Update Your Site”
Is Great!! thank you :D
[...] redirects mod_rewrite, 301/401 Redirects & Optimizing Apache Canocial domain 301 redirects 5 htaccess Tricks Every Webmaster Should Know Apache Redirect [...]
great post - thanks!
[...] 5 htaccess Tricks Every Webmaster Should Know [...]
Unable to find .htaccess file in Wordpress. Any ideas?