Apache RewriteCond Directive

RewriteCond Directive

The RewriteCond directive defines a rule condition. One or more RewriteCond can precede RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern and if these conditions are met.
Basicaly the RewriteCond is like if statement in programming. You can use one or more RewriteCond directives before the RewriteRule with flags:

RewriteCond %{REQUEST_FILENAME} -s    [OR]
RewriteCond %{REQUEST_FILENAME} -l

1. RewriteCond Options

The options are used to check the condition against the type of the matched URI

  • d – is directory
  • f – is regular file
  • s – is regular file with size
  • l – is symbolic link
  • F – is existing file
  • U – is existing URL

2. MIME Headers

Here %{REQUEST_FILENAME} is server variable. All there server variables are MIME-headers. There is a list of the server variables:

HTTP headers: HTTP_USER_AGENT, HTTP_REFERER, HTTP_COOKIE, HTTP_FORWARDED, HTTP_HOST, HTTP_PROXY_CONNECTION, HTTP_ACCEPT
Connection & Request: REMOTE_ADDR, REMOTE_HOST, REMOTE_PORT, REMOTE_USER, REMOTE_IDENT, REQUEST_METHOD, SCRIPT_FILENAME, PATH_INFO, QUERY_STRING, AUTH_TYPE
Server Internals: DOCUMENT_ROOT, SERVER_ADMIN, SERVER_NAME, SERVER_ADDR, SERVER_PORT, SERVER_PROTOCOL, SERVER_SOFTWARE
System Stuff: TIME_YEAR, TIME_MON, TIME_DAY, TIME_HOUR, TIME_MIN, TIME_SEC, TIME_WDAY, TIME
Specials: API_VERSION, THE_REQUEST, REQUEST_URI, REQUEST_FILENAME, IS_SUBREQ, HTTPS

3. RewriteCond Server Variables

Here is a list of MIME headers special to mod_rewrite

IS_SUBREQ
Will contain the text “true” if the request currently being processed is a sub-request, “false” otherwise. Sub-requests may be generated by modules that need to resolve additional files or URIs in order to complete their tasks.
API_VERSION
This is the version of the Apache module API (the internal interface between server and module) in the current httpd build, as defined in include/ap_mmn.h. The module API version corresponds to the version of Apache in use (in the release version of Apache 1.3.14, for instance, it is 19990320:10), but is mainly of interest to module authors.
THE_REQUEST
The full HTTP request line sent by the browser to the server (e.g., “GET /index.html HTTP/1.1″). This does not include any additional headers sent by the browser.
REQUEST_URI
The resource requested in the HTTP request line. (In the example above, this would be “/index.html”.)
REQUEST_FILENAME
The full local filesystem path to the file or script matching the request.
HTTPS
Will contain the text “on” if the connection is using SSL/TLS, or “off” otherwise. (This variable can be safely used regardless of whether or not mod_ssl is loaded).

4. RewriteCond Flags

There is also special flags [NC] (no case) and [OR] used in the RewriteCond.

5. Other Flags

B - The B flag escapes the backreference $1 so that it can safely be used as a query paramater
QSA - The QSA flags appends all the query parameters from the original request to the rewritten request
L - The L flag is not strictly necessary, but avoids evaluating other rewrite rules when this one is matched

Here is the full list.

Examples

1. Rewrite Rule Based on Condition

RewriteCond %{REQUEST_FILENAME} -s   [OR]
RewriteCond %{REQUEST_FILENAME} -l   [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ /index.php [NC,L]

If REQUEST_FILENAME is regular file with size or symlink or directory use ^.*$ pattern to match the URI and rewrite it to index.php in the server root directory.

2. RewriteRule based on Browser condition

RewriteCond  %{HTTP_USER_AGENT}  
^Mozilla.*RewriteRule  ^/$                 /homepage.max.html  [L]
RewriteCond  %{HTTP_USER_AGENT}  
^Lynx.*RewriteRule  ^/$                 /homepage.min.html  [L]
RewriteRule  ^/$                 /homepage.std.html  [L]
Tagged with: , , ,
Posted in Apache

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Sites
Categories
Archives