Log in / create account | Login with OpenID
DocForge
Programmer's Wiki

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.

[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.

  1. RewriteBase /
  2. RewriteCond %{REQUEST_FILENAME} !-f
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
  1. Set the path at which the URL scheme begins. The application runs from the root of the relative URL.
  2. 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.
  3. The second condition stops analysis if the request is the exact path of an existing directory.
  4. 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".

Do you have information or insights to contribute to this article? Please feel free to edit this page. Ask questions or contribute to the discussion on this article's talk page.