17

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

Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213

3 Answers3

20
CheckSpelling on

Matches files and directories. See the documentation for details.

fuxia
  • 62,923
  • 6
  • 54
  • 62
  • Is there an equivalent for the more recent versions? – icedwater Oct 04 '13 at 04:23
  • Didn't seem that way; I have Ubuntu 12.04 LTS, Apache 2.2.22 and creating symbolic links to speling.load and speling.conf in `$APACHE/mods-enabled/` doesn't seem to allow folder names with the wrong case. (of course, speling.load and speling.conf are set up... maybe I should post another question.) – icedwater Oct 04 '13 at 07:47
  • OK. It was me being silly with the symbolic links. Problem solved. – icedwater Oct 06 '13 at 16:55
4

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.

Maelstrom
  • 2,828
  • 1
  • 18
  • 12
  • With `mod_rewrite` a malformed request like `/SoMeThinG.jpg` to a particular capitalization like `/SomeThing.jpg` ain't possible, right? `mod_speling` would be perfect for my use case of serving mixed case media files case-insensitive. The Shared Webhosting of my webhost uses Apache but `mod_speling` is off. Another webhost where I inquired for their Shared Webhosting uses Litespeed which has no `mod_speling` support at all. Seems `mod_speling` is not common in Shared Webhosting. Hence I look for an alternative way within `.htaccess` with other Apache modules. – porg Feb 20 '23 at 21:39
  • As this is probably too much for a comment thread (that's 12y old) I asked this as a proper standalone question here: [Serve mixed case files case-insensitive on Linux Apache without mod_speling?](https://serverfault.com/questions/1123372/) – porg Feb 20 '23 at 22:27
0

Look up the Apache module mod_negotiation. It does exactly what you want: http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html#multiviews

You can also pipe all requests to a single PHP file and let the PHP file do the checking for you.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
  • That's close but seems mostly for adding extensions. Like a request for `foo` becomes `foo.xml` or `foo.html`. I'm trying to fix typos. – Paul Tarjan Feb 02 '10 at 07:03