2

I think this is normally done with .htaccess or httpd.conf, but it's a bit cumbersome on shared hosting, and it seems like a simple trick for a CMS...

Can Craft convert requested URLs to lowercase? So if the slug for the page is /meditate, but someone types in /Meditate that Craft delivers /meditate page?

I don't quite understand how one uses customAsciiCharMappings -- it seems unrelated to this.

3

Depending on how your server is configured, this may be a non-issue. I just checked several sites, and they all work fine with mixed-case URLs.

If that's not the case with your site, you'll need to implement a simple plugin, which checks the URL, converts it to lowercase, and redirects the visitor (if required).

Hopefully it goes without saying that it would be much better to do this in your .htaccess or httpd.conf.

Caveats dispensed with, here's an example plugin.

<?php namespace Craft;

class PoorMansRedirectPlugin extends BasePlugin
{
    // Standard plugin methods omitted for brevity.


    /**
     * Runs before *every* request.
     */
    public function init()
    {
        $path = craft()->request->getPath();
        $lowerPath = strtolower($path);

        // If the current path is not lowercased, redirect.
        if ($path !== $lowerPath) {
            $url = UrlHelper::getUrl($lowerPath);
            craft()->request->redirect($url);
        }
    }
}

Just to be clear, this is a really bare-bones solution.

It doesn't make any distinction between site URLs and admin URLs, doesn't account for query strings, will break POST requests, and a lot more besides.

As such it is in no way production ready, and would require quite a bit of additional work to account for all of these edge (and not so edge) cases. See my previous note regarding .htaccess being vastly preferable.

  • Thank you very much for the clarification. On our server, the case needs to be exact. I never thought about messing up POSTs, and it turns out the .htaccess fix for it, though long, isn't noticibly slow. So .htaccess it is! – kr37 Apr 17 '17 at 0:39
1

This is the .htaccess which is working for me now. From this site

Htaccess to Redirect Uppercase to Lowercase

This should go at the very top of your .htaccess file. At least it should go above ANY other RewriteRules. That is because this uses a loop, until there are no more uppercase characters to convert, it will keep starting at the first HASCAPS:TRUE RewriteRule. Oh, and this is actually really quick and isn't gonna slow down anything.

RewriteEngine On
RewriteBase /

# If there are caps, set HASCAPS to true and skip next rule
RewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

# Skip this entire section if no uppercase letters in requested URL
RewriteRule ![A-Z] - [S=28]

# Replace single occurance of CAP with cap, then process next Rule.
RewriteRule ^([^A]*)A(.*)$ $1a$2
RewriteRule ^([^B]*)B(.*)$ $1b$2
RewriteRule ^([^C]*)C(.*)$ $1c$2
RewriteRule ^([^D]*)D(.*)$ $1d$2
RewriteRule ^([^E]*)E(.*)$ $1e$2
RewriteRule ^([^F]*)F(.*)$ $1f$2
RewriteRule ^([^G]*)G(.*)$ $1g$2
RewriteRule ^([^H]*)H(.*)$ $1h$2
RewriteRule ^([^I]*)I(.*)$ $1i$2
RewriteRule ^([^J]*)J(.*)$ $1j$2
RewriteRule ^([^K]*)K(.*)$ $1k$2
RewriteRule ^([^L]*)L(.*)$ $1l$2
RewriteRule ^([^M]*)M(.*)$ $1m$2
RewriteRule ^([^N]*)N(.*)$ $1n$2
RewriteRule ^([^O]*)O(.*)$ $1o$2
RewriteRule ^([^P]*)P(.*)$ $1p$2
RewriteRule ^([^Q]*)Q(.*)$ $1q$2
RewriteRule ^([^R]*)R(.*)$ $1r$2
RewriteRule ^([^S]*)S(.*)$ $1s$2
RewriteRule ^([^T]*)T(.*)$ $1t$2
RewriteRule ^([^U]*)U(.*)$ $1u$2
RewriteRule ^([^V]*)V(.*)$ $1v$2
RewriteRule ^([^W]*)W(.*)$ $1w$2
RewriteRule ^([^X]*)X(.*)$ $1x$2
RewriteRule ^([^Y]*)Y(.*)$ $1y$2
RewriteRule ^([^Z]*)Z(.*)$ $1z$2

# If there are any uppercase letters, restart at very first RewriteRule in file.
RewriteRule [A-Z] - [N]

RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.