Software Development Resources
Create account | Log in | Log in with OpenID | Help

Mod rewrite

From DocForge

Note that due to technical limitations this article is actually titled mod_rewrite

mod_rewrite is an Apache httpd module which modifies URLs and responds to certain URL requests. Based on a configured set of rules, the web server can alter the URLs before being passed to a web application. The module can also respond to some requests before they make it to the web application.

Contents

[edit] Configuration

[edit] Examples

Assume the following configuration for each example:

  • DocumentRoot is /var/www/htdocs/
  • Domain is example.com

[edit] Everything Handled By Index.php

This is an example taken from the Drupal project and found in the .htaccess file in the root of the application.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
  1. Turn on the rewrite engine.
  2. Set the path at which the URL scheme begins. The application runs from the root of the relative URL.
  3. The first condition stops analysis if the request is the exact path of an existing file. For example, if the request is for /screen.css and the file /var/www/htdocs/screen.css exists, the following conditions and rules are ignored.
  4. The second condition stops analysis if the request is the exact path of an existing directory.
  5. The rule uses a regular expression to take the entire relative path and pass it to index.php as a query string parameter ("q").

From the perspective of a web application the result is a request for http://www.example.com/welcome will actually execute the index.php script with $_GET['q'] containing the string "welcome".

[edit] Multiple Domains and Subdomains

httpd can serve web requests from multiple domains and subdomains. This is typically configured using virtual hosts. A very common scenario is for a site to have both the main domain plus the "www" subdomain registered. Sometimes multiple domains are registered due to renaming of a site, branding, etc. mod_rewrite should be used to ensure that all content is served from only one domain/subdomain, both for the sake of your end users and for search engine optimization (i.e. avoiding what appears to be duplicate content at multiple URLs).

NameVirtualHost *:80
<VirtualHost *:80>
 ServerName example.com
 ServerAlias www.example.com
 ServerAlias olddomain.com
 DocumentRoot /var/www/htdocs
 RewriteEngine on
 RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
 RewriteRule ^(.*)$ http://example.com$1 [L,R=301]
</VirtualHost>
  1. Inform httpd that named virtual hosts will be used.
  2. Begin the configuration of one virtual host.
  3. Define the main server name this virtual host will respond to.
  4. Define the "www" subdomain as an alias, which this configuration should also respond to.
  5. Define the old site's domain as an alias, which this configuration should also respond to.
  6. Set the document root for this virtual host. Each virtual host can have a separate document root, allowing completely separate web sites and applications to be served from one Apache instance.
  7. Turn on the rewrite engine.
  8. Set a rewrite condition for the rule to follow. The condition takes the host name which came through the HTTP request and runs it through a regular expression comparison. The [NC] makes the comparison case insensitive. The exclamation point (!) in front of the regular expression negates it. Therefore if the comparison of the host name to the expression is not true, the condition is met and the rule to follow will execute.
  9. Define a rule which will run if the above condition is met. The first parameter is a regular expression which captures the entire URL following the domain name. The second parameter tells mod_rewrite what to do when the regex is met (in this case always): redirect every request to the primary domain name, with the rest of the URL appended. The [L,R=301] indicates it's the last rule to run and the client should get back a 301 ("Permanent") redirect.
  10. End this virtual host configuration.

This configuration produces the following results:

  • All requests to http://example.com stay exactly as they are. httpd will proceed to serve the requested documents through the document root.
  • A request to http://www.example.com/home would immediately redirect the user to http://example.com/home, which would then come back as a new request and be served.
  • A request to http://olddomain.com/oldpage would immediately redirect the user to http://example.com/oldpage, which would then come back as a new request and be served.

Discuss