How to ignore case in regexp mapping in a .htaccess rewrite rule?

58 views Asked by At

What I have now is the following:

RewriteRule ^([^/]+)/*$ x/$1.html [L,NC,END]

This would point:

/abcd  to  x/abcd.html
/ABCD  to  x/ABCD.html

which would not be fine on a Unix file system that is case-sensitive.

What I want is to point both variations to:

x/abcd.html

How can that be achieved?

Similar questions have been asked about this on Stack Overflow, but none seems to have addressed the issue of regexp back referencing on a Unix system.

1

There are 1 answers

10
IMSoP On

Transforming the text in the URL, rather than just substituting it, can be achieved using the RewriteMap directive. The Apache httpd user guide has a page detailing how to use RewriteMap, which includes this example:

Redirect a URI to an all-lowercase version of itself

RewriteMap lc int:tolower
RewriteRule "(.*)" "${lc:$1}" [R]

The first line defines the map "lc" to be the built-in "tolower" function, and the second line applies that map to the back-reference $1 in a rewrite rule.

So in your example, you could use:

RewriteMap lc int:tolower
RewriteRule ^([^/]+)/*$ x/{lc:$1}.html [L,NC,END]