Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive URLs with mod_rewrite

I'd like for any url that doesn't hit an existing file, to do a lookup on the other possible cases and see if those files exist, and if so, 302 to them.

If that's not possible, then I'm ok with these compromises:

  • Only check the lowercase version
  • Only check the first path portion

For example http://example.com/CoOl/PaTH/CaMELcaSE should redirect to http://example.com/cool/path/camelCase (assuming the latter exists).

but of course a full solution is much more useful to me and others

like image 656
Paul Tarjan Avatar asked Jan 04 '10 08:01

Paul Tarjan


2 Answers

CheckSpelling on

Matches files and directories. See the documentation for details.

like image 138
fuxia Avatar answered Oct 12 '22 10:10

fuxia


I don't have Apache handy to test, but some combination of these rules should do what you want:

RewriteEngine on
RewriteMap lower int:tolower
RewriteCond ${lower:%{REQUEST_URI}} -U
RewriteRule [A-Z] ${lower:%{REQUEST_URI}} [R=302,L]
  • A lowercase map to convert /SoMeThinG to /something
  • A condition to see if the lowercase of the REQUEST_URI exists (-U is internal apache query)
  • The rule to actually do the rewrite

I don't know if the RewriteMap can be applied in a condition, or if it only applies to a rule. These are based on experts exchange accepted answer and a small orange forum discussion.

Your "ideal" solution is probably not possible unless you can enumerate every valid page on your site. If you only have a few valid pages, a combination of RewriteMap and a text map will do exactly what you need. If there are hundreds / thousands of pages you may need to write a script and use the prg directive.

If you can't identify every valid page, you would need to try every variant in case. Consider your URL as a binary string, with 0 for lowercase letter and 1 for uppercase. Just from your simple example you'd have to test 2^17 variations, 128k pages.

like image 4
Maelstrom Avatar answered Oct 12 '22 10:10

Maelstrom