apache.htaccessmod-rewrite

Is it possible to use Apache mod_speling without a 301 redirect? The original URL should be preserved and the file should be served regardless of case


I am using Apache 2.4.37 on a fairly simple server that is just serving images. The human client that requested this service be setup with case insensitivity. Initially I implemented mod_speling like this:

<IfModule mod_speling.c>
    CheckSpelling On
    CheckCaseOnly On
</IfModule>

And it seemed to work great!

But we hit a snag. The crappy app (nuff said) this service was setup for chokes on 301 redirects and can’t be adjusted. So what I am looking for is a way via mod_rewrite to affect case insensitivity.

Initially, I came across this Server Fault answer was that seemed ideal with his RewriteRule:

RewriteEngine On
RewriteMap lc int:tolower
RewriteRule ^(.*) ${lc:$1}

But it’s not appropriate in my setup because we use images with uppercase filenames and lowercase file extensions like this:

A1234.jpg

And that RewriteRule would rewrite the filename to:

a1234.jpg

Which would mean the file would not be found with a 404 error.

So is there an Apache RewriteRule that can be set to force any/all permutations of the filename to an uppercase file name and a lowercase file extension so the image can be loaded without a 301 redirect?

So the filename in URLs could be any of the following:

a1234.jpg
A1234.JPG
A1234.JpG
A1234.jpg

And the proper A1234.jpg would be served by Apache without a 301 redirect? Or is there a way for Apache to use mod_speling without a 301 redirect and preserve the original URL?


Update: Please note that the actual filenames can’t all be converted to lowercase. They must stay as all uppercase for the filename with a lowercase extension.

Also note that the source files are on a Red Hat 8 filesystem (ext4) that is case sensitive and cannot be changed.

Additionally, the data referring to these files cannot be normalized. I am simply trying to deal with a situation and setup I cannot control (past Apache) as best I can with the systems staying as-is. This is a real world scenario that many of us have to deal with on a daily basis; we all can’t adjust systems at will to be perfect.


Solution

  • I figured it out!

    And no, to the best of my knowledge it is not possible to use Apache mod_speling without a 301 redirect. That is just the way mod_speling works.

    That said, I was able to work some magic with some basic Apache RewriteRule logic.

    I just had to set these RewriteRule related items in the main Apache config for the VirtualHost and all works as expected:

    RewriteEngine on
    RewriteMap uc int:toupper
    RewriteMap lc int:tolower
    RewriteRule ^(.+)\.(\w+)$ ${uc:$1}.${lc:$2} [L]
    

    Now any file name permutation such as:

    a1234.jpg
    A1234.JPG
    A1234.JpG
    A1234.jpg
    

    Will properly return the actual source file: A1234.jpg