• Resolved abitofmind

    (@abitofmind)


    Say the media file’s URL is:
    /wp-content/uploads/My-Image-File.jpg

    By what means can I achieve that if someone requests:
    my-image-file.jpg or my-image-file.JPG or any other of those permutations (=identical except capitalization)

    that this request gets redirected (HTTP 3xx) to the real canonical filename My-Image-File.jpg and then loaded from there normally?

    In whose domain / responsibility is this? Ideally?

    • Webserver (in my case Apache)
    • WordPress Core
    • Your Redirection plugin

    And how do I concretely set it up (possibly in each of those components, or in interplay if a combination is needed)?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author John Godley

    (@johnny5)

    Unless you have a specific problem with certain URLs this is not something you really need to worry about.

    Thread Starter abitofmind

    (@abitofmind)

    I’d like to use filenames with some mix of upper and lowercase to denote meaning and when downloaded that the person has them in that form too.

    And get a redirect to that mixed case filename if:
    a) I tell a URL to someone orally and that person types the URL all in lowercase,
    b) shared through a campaign/mailletter service or similar which does not preserve case and enforces lowercase (saw that already!)
    c) or the person entering a URL in a CMS or sharing service transforms the case by incidence or because he/she thinks “URLS must generally be all lowercase, I will transform it to lowercase”.

    So as I hopefully established to you that I have real reasons behind this inquiry, how/where can I achieve that redirection to happen? Do you know how to do that? Or can refer me to any other sources/expert?

    Thread Starter abitofmind

    (@abitofmind)

    Ofc the safest bet would be to say “let’s work with filenames that are all lowercase and then I am on the safe side”.


    I could transform them prior uploading to WordPress. Or use the Media File Renamer plugin which in addition to removing spaces with dashes (which WP does) additionally also transforms all characters to lowercase.

    But I’d prefer to keep the semantic extra information that mixed case carries, if I can establish a solutions that ensures any case permutated URLs reach their canonical mixed case URL.

    Plugin Author John Godley

    (@johnny5)

    As with your other thread I don’t think this is necessary and you are likely to cause problems elsewhere.

    If you do still want to carry on then you can use the advice in the other thread (a regular expression), or look at getting your server to do the transformation.

    Thread Starter abitofmind

    (@abitofmind)

    The basic WordPress htaccess is this:

    # BEGIN WordPress
    
    RewriteEngine On
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    
    # END WordPress


    From this I deduced the following:

    A request only gets to WordPress index.php and subsequently the WordPress routing if there is not file (-f) or directory (-d) that matches the request.

    In the WordPress HTML output the source attribute of the media files are URLs which directly correspond to filepaths.

    So when the web browser requests media files, they are handled entirely by the webserver (Apache) and WordPress (and its PHP filesystem I/O routines) is not involved in serving the media files as I see it.

    So the case insensitivity can be achieved entirely by the proper .htaccess directives. And there happens to be a solution in Apache exactly for the purpose of case insensitive URLs which is mod_speling:

    ## Case insensitive file serving
    
    ### mod_speling
    # attempts to correct mistaken URLs by ignoring capitalization,
    # or attempting to correct various minor misspellings.
    
    ### Enable the spelling module
    CheckSpelling on
    
    ### Limit the action of the speling module to case corrections - I solely need this.
    CheckCaseOnly on
    
    ### Also match files with differing file name extensions - I explicitly don't want this. Want to avoid unintentionally serving image.gif instead of image.png
    CheckBasenameMatch off

    I put this into the .htaccess of the subdomain which serves my Media Library. Alternatively you may also put this into the default Media Library location at /wp-content/uploads/ to limit mod_speling to that directory and its subdirectories.

    Only problem: Does (yet) not work. My webhost seems to have something in the global httpd config which forbids CheckSpelling On. Already asked them. When I got a solution I will inform you here.

    Thread Starter abitofmind

    (@abitofmind)

    Answer by my webhost:

    • Case insensitive file names are possible with such rulesets.
    • But mod_speling is not included in Shared Webhosting.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can Redirection plugin also achieve to serve media filenames case insensitive?’ is closed to new replies.