Apache rewrite URIs to lower case

Chris Hirst's Avatar
Written by Chris Hirst. Posted in Articles on 17 February 2014.
Hot 1635 hits 0 favoured

With a web site that is being served by Apache running on a Unix or Linux operating system the document and directory names are case sensitive, ie: Page.ext, page.ext, page.EXT are all seen as different resources so if someone links to a URL on your website with an uppercase letter in the href attribute, and if all URIs on the site are lower case the link will fail with a HTTP: 404 (Not Found) error response, so provided you have followed good practice and ensure that your URLs are all lowercase letters, there are two ways that you can use Apache features that are not widely known or used by developers one is the RewriteMap directive. This 'URL mapping' directive can be used when the rewrite or redirection rules are just too complex or diverse to be handled by a regular expression

1: Force Lower case with RewriteMap.

This is not the best method or the recommended way for case correction but is included for completeness and as an example of RewriteMap.

 

RewriteEngine On
RewriteMap lcase int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lcase:$1} [R=301,L]

To explain the directives further,;

RewriteEngine On - this activates the Apache runtime rewriting engine for the rewrite rules and conditions that follow the 'On' instruction. The default is for the engine to be off so if this is the first set of conditions in the .htaccess file, it is required.

RewriteMap sets the map filename and the key of the entry in the map to be applied.

'lcase' is the map name and is used later in the rules to substitute matches.
int: indicates that the mapping is an internal function, in this case the tolower function

The RewriteCond sets a conditional rewrite if any upper case [A-Z] characters are in the resource that is being requested by the user-agent.

The RewriteRule sets a rule that will use the specified map (lcase) to redirect the user-agent to.

 

2: Force URL request to be case insensitive with mod_speling.

[The spelling of the module name is correct]

This method is to be preferred especially for a high traffic web site as it is less server intensive, simply because it only requires one HTTP request whereas rewrites and redirects require two request, one external (THE_REQUEST) and one internal request for the URL that is served to the user-agent. This directive has other advantages in that it can be applied server wide as a directive in httpd.conf, to specific vhosts in httpd.conf, and in .htaccess to be 'site' wide or to specific directories. the configuration is:

CheckCaseOnly on|off

The instructions are fairly self explanatory insofar as 'on' enables the server to be case insensitive, while off is the default of being case sensitive. What it does is to check the document names with no differentiation between upper case and lower case characters and return the document name or document names found.

If only one match is found this will be served out to the requesting user agent, if more than one document matches a list will be presented for the user to choose from. This module can do much more than simply correct for case and can be looked at in more detail in the Apache documentation https://httpd.apache.org/docs/2.2/mod/mod_speling.html


Chris Hirst's Avatar

Author: Chris Hirst

36 0 0
...
Powered by CjBlog