DNS Lookup, Reverse DNS Lookup, Domain Configuration Check and IP Address Calculators

Using Mod_Rewrite (Continued)

Hiding File Extensions

If you want to hide your file extensions from the user, you could use rules like these:

Options +FollowSymlinks
rewriteEngine on

#Check if file exists with .pl extension
rewriteCond %{REQUEST_FILENAME}.pl -f
rewriteRule (.+) /$1.pl [L]

#Check if file exists with .html extension
rewriteCond %{REQUEST_FILENAME}.html -f
rewriteRule (.+) /$1.html

These rules take a URL without an extension, check to see if a file exists on the server with either a .pl or .html extension. If a file is found then it will silently rewrite it to use that file.

These rules allow you to keep extensions on your files, but hide them from the user.

The [L] option specifies that this is the last rule (if this rule was actioned). This means that if this rule is actioned, all further rewrite rules are skipped. If you want to specify multiple options they can be separated by a comma (eg. [R,L]).

Dynamic Pages

Dynamically generated pages may take parameters, like an article or knowledge base ID (eg. script.pl?article=mod_rewrite). You could use rewrite rules to convert page names into URL parameters. This hides the URL parameters from the user - they only see a clean URL. For example, the mod_rewrite engine can convert the last path element into a URL parameter of the previous path element.

Here's an example of a rewrite rule to do this.

Options +FollowSymlinks
rewriteEngine on

#Redirect pages beginning with /articles/
#and pass though the page name as a parameter
#to script.pl
RewriteRule ^articles/([^/]+)$ /script.pl?article=$1 [NC]

This rewrite rule will turn




But the original URL will remain displayed in the browser.

The [NC] options specifies that this match is not case sensitive.


To troubleshoot rewrite rules you may want to enable logging. The following commands will turn on logging for the mod_rewrite module.

Options +FollowSymlinks
rewriteEngine on

RewriteLog "/etc/httpd/logs/rewrite.log"
RewriteLogLevel 3

Note that the rewrite log cannot be specified in the .htaccess file. It can only be specified in the httpd.conf file.