Module mod_rewrite is one of the most popular module in the Apache web server that is for rewriting and redirecting URLs dynamically. It’s based on regular expression with using powerful pattern matching to allow for handling of very complex situation. So, with this module, we create custom URL, clean and search engine friendly ( SEO ), Google will be easy to index our web pages. Others reason if we use this module, we make ugly URLs more attractive and URLs is easier to type, easier to remember, easier to tell someone / friend.

The first example in this discussion, we will build web site with complicated and has some template dynamically. In this case, we need redirect our public files into specific directory in the web root directory and template directory, such as css files, javascript files and images files. We can use mod_rewrite and create .htaccess file in the root web directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
	<IfModule mod_rewrite.c> 
 
	RewriteEngine On
    	RewriteBase /
 
	RewriteRule ^public/([a-zA-Z_-]*)/(.*)$ /www/template/$1/static/$2 [L]
 
    	RewriteCond %{REQUEST_FILENAME} !-f
    	RewriteCond %{REQUEST_FILENAME} !-d
    	RewriteRule ^(.*)$ index.php/$1 [L]
 
</IfModule>

In the above example code, we assume have a URL like this :

http://mysite.com/public/style/style.css

for css file that it can be accessed publicly. If our client access this URL, our client will be redirected to our web directory like this : /www/template/style/style.css.

In other situation also can use this case. For example, if we have URL like this :

http://mysite.com/public/javascript/jquery.js

for JavaScript file and this file can be accessed publicly. In this case, our client will be redirected to our web directory like this : /www/template/javascript/jquery.js.

With this script, we may implement this case in more complicated case for make clean URL and SEO friendly.

In the next example, we will rewrite path information to a query string. In this case, we have a URL like this :

http://mysite.com/products.php?shoes

. With this URL, we want to make it to look like this :

http://mysite.com/products/shoes

the URL make clean and SEO friendly. In this case, we can use script in the file .htaccess like script below.

1
2
3
4
5
6
7
8
9
 
	<IfModule mod_rewrite.c> 
 
	RewriteEngine On
    	RewriteBase /
 
	RewriteRule ^products/(.*)$ products.php?$1 [PT]
 
</IfModule>

With this rule will be that anything appearing after “products/” will be put into the query string and will be redirected to “products.php?”. So, how to create .htaccess file if we want to rewrite path information to a complicated query string like this : http://mysite.com/products?basecolor=black&mansize=44 and we want the URL like this : http://mysite.com/products/black/44 …?

In the end of this discussion, we can conclusion that module mod_rewrite in Apache is powerful and very effective to rewriting URL.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Slashdot
  • Technorati
  • YahooMyWeb