The Blog

mod_rewrite 101

published May 11, 2010

mod_rewrite is a popular apache module for rewriting urls and making redirects. I decided to write small introduction about it, since I had to configure it on project test server. Reason why we are using it, is our upcoming release in which we will need to forward our customers to new liferay community instead of default /web/guest.

So, after rewrite engine is turned on from apache configuration, it will filter all matched requests and apply special rules for it. Request is passed through this chain of rules and if match is found, redirect is sent by apache.

mod_rewrite’s work horses are RewriteRule and RewriteCond directives which are used to build rule chain. Processing works so that

1) config files and .htaccess files are parsed for RewriteRules from top to bottom.
2) First RewriteRule is selected and request is compared against RewriteRule’s pattern.
3) If match is found and there are RewriteCond elements introduced, they are tested against the request.
4) If conditions match, redirect to new rewritten url is made.
5) If there was no match, request is passed onwards to next RewriteRule in chain, if such exist.

rewrite

Configurations
Below is a simple configuration for redirect

# Turn on rewrite engine processing
RewriteEngine On
# Write log here
RewriteLog /var/log/httpd/rewrite.log
# Log everything
RewriteLogLevel 9
# Redirect context root to new community /web/new/
RewriteRule ^/$ http://%{SERVER_NAME}/web/new/

# Do not allow access to old /web/guest
RewriteRule ^/web/guest(.*)$ http://%{SERVER_NAME}/web/new/

There is loads of stuff you can do with this module, check it out.

Samuli @ 19:59 (One Comment)

tags: ,

Trackback URL

One Comment to mod_rewrite 101
snonlelottomy says on July 9, 2010 @ 13:29:08:

learned a lot

top











b

Subscribe to RSS feed

The Tag Cloud
The Blog Archive

February 2012 (1)

January 2012 (1)

November 2011 (1)

June 2011 (2)

May 2011 (1)

April 2011 (2)

March 2011 (2)

February 2011 (1)

January 2011 (1)

December 2010 (1)

November 2010 (1)

October 2010 (3)

September 2010 (3)

August 2010 (5)

July 2010 (2)

June 2010 (3)

May 2010 (4)

April 2010 (2)

March 2010 (6)

February 2010 (7)

January 2010 (3)

December 2009 (7)

November 2009 (6)