mod_rewrite 101

March 11th 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.

modrewrite's work horses are RewriteRule and RewriteCond directives which are used to build rule chain. <a href="">Processing</a> 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.

<img class="blog-image" title="rewrite" src="/img/blog/rewrite.png" alt="rewrite"/>


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, <a href="">check it out</a>.

What's next?

Did you know that I provide software development consulting services? Check out how I can help you.

« Back to blog