Skip to main content
ApacheSUSE

Using mod_rewrite with Apache HTTPD server so users can omit file extensions

By October 19, 2013No Comments


The first thing to understand it that we will never understand users, so when we have requests that users forget to add the html extension to the end of web addresses we should really think that we were wrong to expect that they would.

It can be an easy job with Apache to create rewrite rules that redirect the request to the .html page. This will be done with the Apache module mod_rewrite.so so this will need to be loaded if it is not already. In SUSE we set this in /etc/sysconfig/apache2 but this varied across distributions.

Firstly we need to check that this is not a directory:

ReWriteCond %{REQUEST_FILENAME} !-d

The condition here is evaluating the special variable REQUEST_FILENAME and ensuring it is not a directory, the ! negates the -d checking for a directory. Other check we can perform include:

  • -d‘ (is directory)
    Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
  • -f‘ (is regular file)
    Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
  • -F‘ (is existing file, via subrequest)
    Checks whether or not TestString is a valid file, accessible via all the server’s currently-configured access controls for that path. This uses an internal subrequest to do the check, so use it with care – it can impact your server’s performance!
  • -H‘ (is symbolic link, bash convention)
    See -l.
  • -l‘ (is symbolic link)
    Treats the TestString as a pathname and tests whether or not it exists, and is a symbolic link. May also use the bash convention of -L or -h if there’s a possibility of confusion such as when using the -lt or -le tests.
  • -L‘ (is symbolic link, bash convention)
    See -l.
  • -s‘ (is regular file, with size)
    Treats the TestString as a pathname and tests whether or not it exists, and is a regular file with size greater than zero.
  • -U‘ (is existing URL, via subrequest)
    Checks whether or not TestString is a valid URL, accessible via all the server’s currently-configured access controls for that path. This uses an internal subrequest to do the check, so use it with care – it can impact your server’s performance!
  • -x‘ (has executable permissions)
    Treats the TestString as a pathname and tests whether or not it exists, and has executable permissions. These permissions are determined according to the underlying OS.

We then pass the the ReWrite rule IF it is not a directory:

RewriteRule ^([^.]*[^./])$ /$1.html

We start this the anchors. These surround the parentheses.

^ …. $

The carat ^ is the start of the string and $ the end. These surround the round brackets or parentheses . These create the variable $1 that will match the file name in the request. Although the regular expression look a little untidy it is not matching file names that have a slash at the end or any amount of dots in the name. So we will match sales but not sales.html are sales/