Examples on mod_rewrite


1. Description – Your current pages are called using index.php with parameter of url i.e
http://www.example.com/index.php?url=category
and instead of this URL, you want a nice and easy to read URL like http://www.example.com/category
Solution – Put the following lines in your .htaccess file.
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?url=$1 [L]

Note: If the .htaccess file already contains a line ‘RewriteEngine on’ then you don’t need to put it again unless it was set to off before you putting in your lines.

2. Description – Your current URL is
http://www.example.com/index.php?cat=category&subcat=subcategory
which you would like to see as
http://www.example.com/category/subcategory
Solution – Put the below lines in your .htaccess file
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?cat=$1&subcat=$2 [L]


3. Description – You want to have many sub categories or categories like
http://www.your-domain.com/category/subcat1/subcat2/subcat3/subcat4/subcat5/
which you would to rewrite to
http://www.your-domain.com/index.php?cat=category&subcat1=subcat1&subcat2=subcat2 and so on …
Solution – See below lines..
domain.com/category –> index.php?cat=category
RewriteRule ^([^/\.]+)/?$ /index.php?cat=$1 [L]
domain.com/category/subcategory/ –> index.php?cat=category&subcat=subcategory
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?cat=$1&subcat=$2 [L]
domain.com/p1/p2/p3/ –> index.php?a=p1&b=p2&c=p3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?a=$1&b=$2&c=$3 [L]
domain.com/p1/p2/p3/p4 –> index.php?a=p1&b=p2&c=p3&d=p4
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?a=$1&b=$2&c=$3&d=$4 [L]

4. Description – Your URL has a folder and you would like rewriting for that folder. The URL looks like this http://domain.com/folder/index.php?url=name which you want to see as http://domain.com/folder/name/
Solution – Place the following lines in your .htaccess file
RewriteEngine on
RewriteRule ^folder/([^/\.]+)/?$ folder/index.php?url=$1 [L]


5. Description – Your actual URL is http://example.com/index.php?page=hello which you want to see as http://example.com/hello.htm
Solution – Place the following lines in your .htaccess file
RewriteEngine on
RewriteRule ^([^/\.]+).htm$ index.php?page=$1 [L]


6. Description – Your URL is http://example.com/folder/index.php?page=hello which you want to see as http://example.com/folder/hello.htm
Solution – Place the following lines in your .htaccess file
RewriteEngine on
RewriteRule ^folder/([^/\.]+).htm$ folder/index.php?page=$1 [L]

There are many more things that you can do with mod_rewrite. As and when I discover more examples, I will keep updating this page. Please feel free to post your usage of mod_rewrite if already not covered here and I will add them to the above list of examples.

Presenting custom error pages

Use .htaccess file to present users with your custom pages for 401 [Authorization Required], 403 [Forbidden], 404 [not found] and 500 [Internal Server Error].
Syntax:
ErrorDocument < error-code > < location -of-custom-page>
Examples:
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html

Some script can be included in the customized pages to automatically send an email to you whenever those pages are called for. This way you will be notified every time a user encounters 404, 500 and other error messages.

Redirecting your-domain.com[non-www] to www.your-domain.com

From a search engine’s optimization point of view, we will use permanent redirection such that every time a request for non-www domain is made, it’s redirected and returns a status code of 301 [Permanent Redirect].
RewriteEngine On
RewriteCond %{HTTP_HOST} ^YourSite.com [nc]
RewriteRule (.*) http://www.YourSite.com/$1 [R=301,L]
In the above lines, the first line tells the RewriteEngine to be On, second line mentions the Rewrite Condition and the last one is the rewrite rule.

Comments

Popular posts from this blog

Puzzle

Importing Java Library