How to use mod_rewrite in .htaccess - Simple tutorial

Here is my little experience on using mod_rewrite and .htaccess:

Environment: Ubuntu 6.10 - Apache 2.0.55 + PHP5

To start using mod_rewrite:

1. In your httpd.conf (In my case: apache2.conf), you will notice a line

AccessFileName .htaccess

Please ensure this line exist in your httpd.conf anywhere.

2. Still in httpd.conf: Find out your web root directory defined in <Directory> tags. This is the directory where you would place your .htaccess file in. Once your .htaccess file, which will be made later, is put in this directory, it will take effect immediately, without restart/reload your apache2.

Please find out the following line:

AllowOverride None

and change it to

AllowOverride All

This make the text(directive) which is defined in .htaccess work. AllowOverride means "What text(directive) is allowed to be override by .htaccess?". So, if AllowOverride is "None", it means that .htaccess cannot override anything = useless.

3. At anywhere in your httpd.conf, put the following lines:

RewriteLog /path/to/your/log/file/≶your_log_file_name>
RewriteLogLevel 2

It's ok even if you haven't create this file yet, it will be created automatically.
Log level is 2 here, If you want to see simple log, please enter 1. If you want detail log, enter a higher number....

4. Create a .htaccess file in your web root:

vi .htaccess

and enter the following text

RewriteEngine On
RewriteRule ^info\.php$ http://www.google.com

The first line defined that the mod_rewrite engine should be turned on. This line is only required once per .htaccess file.

The second line defined the rule set. Preceding with "RewriteRule <From> <To>", you can enter regular expression here to "rewrite" your URL. In this example, if you enter http://localhost/info.php, you will be redirected to http://www.google.com

To see the details on how to use mod_write and why using this, please refer to the following website:

http://httpd.apache.org/docs/1.3/howto/htaccess.html

Some of my reference in this blog:

http://www.issociate.de/board/post/295630/Troubles_w/mod_rewrite.html

URL rewriting:

http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

Comments

Popular posts from this blog

TCPDF How to show/display Chinese Character?

Wordpress Load balancing: 2 web servers 1 MySQL without any Cloud services

Using wget bypass htaccess username password 401 authorization