5 htacsess tricks for every webmaster

web hack htaccess webmaster

In the Apache web server, .htaccess (hypertext access) is the default name of directory-level configuration files. A .htaccess file is placed in a particular directory, and the directives in the .htaccess file apply to that directory, and all subdirectories thereof. It provides the ability to customize configuration for requests to the particular directory. The file name starts with a dot because dot-files are by convention hidden files on Unix-like operating systems.

1. Custom 404 Error Page


image by smashingmagazine.com

The first trick is about "404 File Not Found" server error page. We can put whenever we want that our visitors see when they tries to access a page on our site that doesn’t exist.

Is very cool that we can replace the server’s default error page with one of our own that explains the error in plain language and links visitors to your home page. Here’s how to use your own page:

Create a .html file with name 404_error.html and add your html code for your custom 404 error page.
Open .htaccess file and add the following code:

ErrorDocument 404 /404_error.html

Upload the 404_error.html page to your root dir and save the .htaccess file. For the last step we need to test this page. Visit your site yoursite.com/blablabla.html something similar and our 404 error page is coming up.

2. Redirect Visitors While You Update Your Site


Here is a hack how to redirect for a short time visitors while we Update and test our web site:

Open .htaccess and add the following code:

order deny,allow
deny from all
allow from

ErrorDocument 403 /offline.html

<Files offline.html>
allow from all

Replace with your IP address. Also replace offline.html with the name of the page you want visitors to see.

3 – Create User Friendly URLs

Which of the two URLs below looks friendlier?


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]

4 – 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_page.html http://yoursite.com/new_page.html

Using a 301 redirect also ensures the page doesn’t lose its search engine ranking.

5 – 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

Share your love

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *