Follow this Blog!

Feb 1, 2010

f Comment

FAQ on Apache .htaccess!

Amazon Simply put, .htaccess if a file you can create in your document root to do all types of handling on a per-directory basis when your server receives a request. Such handling includes what server features are made available, password authentication, URL rewriting, URL redirecting, etc. Suppose you are a book site and your web directory structure looks like this:

/book-name/chapter1/section1/...
You can have .htaccess in /, in /book-name, in /chapter1, etc. each with its own set of configurations that will override the directories higher up in the tree. This allows directory specific configurations. The most common configuration is URL rewriting and URL redirecting.

Make sure "AllowOverride None" does NOT exist in any .htaccess somewhere up in the directory hierarchy. "AllowOverride None" makes Apache ignore your .htaccess!
How do I change Apache .htaccess WITHOUT restarting Apache server?
You do NOT need to restart Apache after you change your .htaccess in order for your change to be reflected. I am using Apache/2.0.52 (CentOS) Server and this is true in this server. If you misconfigure .htaccess you'll see 500 Internal Server Error after you reload the page.

For a comprehensive tutorial visit Apache Tutorial: .htaccess files

How do I quickly 301 redirect a page to another with .htaccess?
Easy. You add the following code in .htaccess:

RewriteEngine on
RewriteRule old-page.html$ http://new-site.com/new-page.html [L,R=301]

Once you save it refresh old-page.html and it should 301 redirect to http://new-site.com/new-page.html.

Before you can do that you need to make sure the mod_rewrite module has been installed and enabled on your Apache server. Most likely this is true. For a comprehensive tutorial on the module visit Apache module mod_rewrite

How come my .htaccess does NOT work?
If after you edit .htaccess you don't get the desired effect, then most likely AllowOverride is messing things up. You may have "AllowOverride None" somewhere up in the hierarchy that makes Apache ignore your .htaccess in question.

One quick way to find out is put junk in .htaccess and fresh your page. If you get no error, that means Apache is ignoring it and you almost certainly have "AllowOverride None" in effect. If you get errors, that means Apache is accessing it and you'll need to fix the errors.
In either case check error log or ask your administrator if you don't have such access.

If you have any questions let me know and I will do my best to help you!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael