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.
Configuration [edit]
- mod_rewrite/RewriteBase - Sets the base URL for per-directory rewrites
- mod_rewrite/RewriteEngine - Turn the rewriting engine on or off
- mod_rewrite/RewriteCond - Set a condition for the next RewriteRule which follows it
- mod_rewrite/RewriteLock - Lock file for RewriteMap programs
- mod_rewrite/RewriteLog - Log to which rewrite actions are written
- mod_rewrite/RewriteLogLevel - Log verbosity level
- mod_rewrite/RewriteMap - Lookup map or application through which mapping can be performed
- mod_rewrite/RewriteOptions
- mod_rewrite/RewriteRule - The pattern and substitution rules to perform the rewriting
Examples [edit]
Assume the following configuration for each example:
- DocumentRoot is /var/www/htdocs/
- Domain is example.com
Everything Handled By Index.php [edit]
This is an example taken from the Drupal project and found in the .htaccess file in the root of the application.
RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
- Set the path at which the URL scheme begins. The application runs from the root of the relative URL.
- 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.
- The second condition stops analysis if the request is the exact path of an existing directory.
- 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".

