5 Great htaccess Tricks That You Can’t Live Without
The .htaccess fileis an extremely powerful tool for your web development arsenal. Learning how to use it effectively will not only improve security but will improve functionality as well.
Weather it’s redirecting visitors to a temporary site, displaying a custom 404 error page or even blocking specific IP Addresses from being able to view your site, here are 5 sets of htaccess directives that every webmaster should know.
It is important to remember that editing the .htaccess file can bring your entire site down. Be sure to have a working back-up before experimenting.
- 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 22.214.171.124 ErrorDocument 403 /visitors-page.html
allow from all
126.96.36.199with your IP address. Also replace
visitors-page.htmlwith the name of the page you want visitors to see.
This allows only your ip address to access the actual website, and all other ip addresses will be redirected to the page of your choice. Pretty slick!
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
404.htmlwith the name of the page that you want your visitors to see.
- Handle Moved or Renamed Pages
This is a trick that I use a lot when I develop a new site. The clients old URL pattern most likely will not match mine, therefore I am able to use a 301 redirect to match and redirect the old URL pattern to the new
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. Most servers are configured to prevent directory browsing like this. However, if yours isn’t, here’s how to set it up:
Options All -Indexes
Using this will ensure that visitors or even search engines will not see files that are not meant for their eyes
Create User Friendly URLs
This is one of my favorites. I’ve created many user friendly URLs whether it be to make it easy to remember or simply to clean up a long URL
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-us.html” whenever anyone visits “http://yoursite.com/about”. Here are a few different types of examples:
RewriteEngine on RewriteRule ^about/$ /pages/about.html [L] RewriteRule ^features/$ /features.php [L] RewriteRule ^issues/$ /issues.html [L] RewriteRule ^contact/$ /pages/contact.htm [L] RewriteRule ^facebook/$ http://facebook.com/myfacebookpage [L]
Notice the last one. What I did there was made it so that instead of going to http://facebook.com/myfacebookpage, you can advertise as http://mywebsite.com/facebook.
I hope these tricks help you out! Remember, there is a lot more that you can do with mod_rewrite and htaccess, I encourage you to explore some more.