Apache Rewrite Mod
mod_rewrite is a powerful module that Apache can utilize.
It is a way of rewriting URLs, modifying the request that Apache recieves. This could be a moved document, or enforcing SSL (rewriting the URL from http to https).
This is a complex subject, which cannot be covered here, but for further information, refer to the full documentation.
Rewrite rules can exist in a .htaccess file, or the main configuration file, or preferably a
mod_rewrite uses Regex (compatible with Perl) for its pattern matching engine. This is nearly unlimited in searching across a range of URLs.
Enable Rewrite Engine
mod_rewrite you should include the following in you Apache Documentation:
A restart of Apache will be required to load the engine.
Declaring a Rewrite Rule
To declare a rule you will have something similar to the code below:
RewriteRule ^/old.html$ new.html [R]
This will redirect a request that Apache receives for old.html page to new.html page. The
^ indicates that it must be the initial part of the request, and the
$ indicates that it is the end of the request. If
/subdir/old.html is passed, it will fail this search pattern as
/subdir/ is at the beginning of the pattern. This is in line with Regular Expressions pattern matching.
These rules can be embedded within a particular directory, using the
<Directory /var/www/html/subdirectory> RewriteEngine on RewriteRule "^old.html$" "new.html" </Directory>
The above rule only applies to the directory named
At the end of each
RewriteRule is a set of flags that determines what should be done - these are enclosed in a set of square brackets. One of the most common is
[R] which is a redirect, carried out at the browser level (issued by the webserver).
A full list of flags is documented at https://httpd.apache.org/docs/2.4/rewrite/flags.html.
Regular Expressions and mod_rewrite
|.||Match any character||c.t matches cat|
|+||Repeats the previous match one or more times||a+ a, aa, aaa|
|*||Repeats the previous match zero or more times||a* matches the same as a+ but will also match an empty string|
|?||Makes the match optional||colou?r will match color and colour|
|\||Escape the next character||. will match a . (dot) and not any single character as explained above|
|^||Called an anchor, matches the beginning of thes string||^a will match a string that begins with a|
|$||The other anchor that matches the end of a string||a$ will match a string that ends with a|
|( )||Groups several characters into a single unit, and captures a match for use in a backreference||(ab)+ matches abababab - the + applies to the group|
|[ ]||A character class - matches one of the characters||c[uoa]t matches cut, cot, cat|
|[^ ]||Negative character class - matches any character not specified||c[^/] matches cat or c=t but not c/t|
mod_rewrite Log Level for < v2.4
mod_rewrite will write to the usual apache log files.
For previous versions (pre Apache 2.4) the directive
RewriteLogLevel will set the level of logging written, ranging in values from 0-9 with 0 being no logging and 0 being the most verbose. Logs will appear as
pass through lines in the file.
mod_rewrite Log Level for v2.4
With the current version of Apache (2.4),
mod_rewrite, the older methods of controlling logging have now been replaced by a new per-module logging method:
LogLevel alert rewrite:trace3