Sunday, April 25, 2010

Mod Rewrite

A quick guide about mod_rewrite module in apache, please check if your server supports mod_rewrite and that u can edit ur htaccess file. for more thorough documentation read here.
basically to use this redirect, there are some line that you need to put in your htaccess.
Click on below links to view more details about this function:
* RewriteEngine
* RewriteBase
* RewriteCond
* RewriteRule
The Definitive Guide to Apache mod_rewrite


Rewrite Engine

RewriteEngine on

Put this before all other rewrite commands to activate apache rewrite module.

Rewrite Base

RewriteBase /baseurl/

Put this to specify the base url for ur website, we use this if ur website is not located in the directly in the root domain folder (ex:your website is located at http://www.z3r0c0d3.com/blog) so you need to specify that the baseurl is blog. Put in RewriteBase /blog, so we can also use this value for later processing.

(example is to create a link, we can put baseurl in front of the link to link to other pages in the website.

Rewrite Rule

RewriteRule <Pattern> <Substitution> <[flags]>

This is the redirection rule that is really working to rewrite the request URL to a new address.

Take note that the order of this rule is important as we can implement combined rule to rewrite a request URL. Unlike the previous redirect command in htaccess, that simply redirect the request when the old_address matches. RewriteRule if the pattern matches, will first rewrite the old url to a new url with the substitution, then according to the flags given, continue to examine the next rule and apply second or more rewrite if the pattern matches. When all rewrite rules has been examined (or until Last flag is found) then the request url will then be transformed to the new url (or being redirected to the new url, according to the flag given).
the Pattern here is using regex (regular expression) to compare whether the request URL matches the pattern or not. important to take note is that we can save group of text in the old url and use it in the new url using () in the pattern, and use $N in the substitute.
example:

RewriteRule ^index.html$ another-link.html [R=301]

this will substitude http://dovestation.blogspot.com/index.html to http://dovestation.blogspot.com/another-link.html

and then redirect the request with 301 response code (defined in flag R=301)
RewriteRule ^.*$ http://www.google.com/another-link.html [R=301]

this will redirect all request in http://dovestation.blogspot.com/ to www.google.com/another-link.html.
RewriteRule ^blog/(.*)/index.html$ another-blog/$1/index.php [R=301]

this will redirect requests in blog folder that ended with index.html, to another-blog folder and use the same path in old request before index.html, and put index.php behind.

http://domain.here/blog/1/2/3/4/asd/index.html will be redirected to http://domain.here/another-blog/1/2/3/4/asd/index.php
RewriteRule ^blog/(.*)/images/(.*)[gif|jpg|png|ico|bmp]$ images/$2jpg [R=301,L]

this will redirect all request to an image inside images folder folder in blog to a jpg image in images folder in root domain with the same filename.

http://domain.here/blog/1/2/3/images/some-picture.gif will be redirected to http://domain.here/images/some-picture.jpg
there are some flags that can be specified for the rule, some commonly used are

R=code which will redirect the request to the new url with specified code.

L is a flag to stop rewriting, (meaning don’t examine further rewrite rule sets) and that this is the last rule applied if match.

N means go back to the first rule and do the process all over again with the new url.

NC means to ignore case when matching the pattern.

Rewrite Cond

RewriteCond <TestString> <CondPattern>

Put one or sets of RewriteCond to help filter Request URL for a RewriteRule. So we can put one, two, or more RewriteCond and if all RewriteCond is matched then RewriteRule would be applied.
Some list that we can use for TestString:
HTTP_USER_AGENT

HTTP_REFERER

HTTP_HOST

REMOTE_ADDR

REMOTE_HOST

REMOTE_USER
REQUEST_METHOD

PATH_INFO

QUERY_STRING

SERVER_NAME

SERVER_ADDR

SERVER_PORT
THE_REQUEST

REQUEST_URI

REQUEST_FILENAME

THE_REQUEST

REQUEST_URI

REQUEST_FILENAME


CondPattern is a regular expression with some additionals:

“-d” is an existing directory

“-f” is an existing file

“!” continued by other CondPattern is to specify a non matching pattern.
example:

RewriteCond %{QUERY_STRING} .*

RewriteRule ^blog/(.*)$ another-blog/$1? [R=301,L]

the RewriteCond will check for every request with/without query string. Inside blog folder will be redirected (RewriteRule) to another-blog. We can put the “?” sign behind to remove the query string from the old url.
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^.*$ index.php [L]

the RewriteCond will check if the request is not an existing file, then rewrite the URL to index.php (but does no redirection because we didn’t specify R flag). so the url in address bar will stay the same, is just that the server will serve index.php to the browser. the RewriteRule won’t be applied if the request points to an existing file. (meaning it will serve the designated file if it’s exists, like images/css/js).



No comments:

Post a Comment