In most cases, case-sensitivity depends on the database collation. By default, Craft uses case insensitive collations (
utf8mb4_0900_ai_ci, depending on the charset). You can change that in your database configuration. Keep in mind that if you change the collation for an existing project, it won't update existing tables automatically. You can run this console command to change all tables to the configured collation:
php craft db/convert-charset
Effects of changing the collation
Using a case-sensitive collation will make ALL database queries against that table case-sensitive. This means that upper- and lower-case letters are no longer considered identical for comparison purposes. Consider a search for elements with the title
apple. Using a case-insensitive collation, the search will find also find
aPpLe. Using a case-sensitive collation, only
apple will match that query. See Case Sensitivity in String Searches for more information.
You can work around this for individual queries in multiple – for example, by performing an individual query with a different collation or casting a specific column to binary (
CAST(title as BINARY)). Though Craft's element query interface doesn't provide fine-grained control for that and you can't modify all queries the core uses to find and display content, so that's probably not a feasible solution.
Changing the collation in your database configuration and updating all tables using the command mentioned above should have the desired effect (unless Craft handles this case in a special way, which I don't think it does). That is, if the URL is stored in lower-case, the same URL with upper-case letters won't match that entry, so that URL will go to a 404 page. This might be different for dynamic routes defined in PHP. Those aren't stored in the database as far as I know, so it depends on how Yii2 handles those under the hood.
However, changing the collation will also impact every other query – for example, your search page might work differently if searching for
apple doesn't find an entry with the title
- People usually don't type URLs manually, and when they do they usually type in lower-case. Instead of preventing upper-case URLs from matching with existing entries, this effort is better spent making sure all the links on your site are in lower-case and nobody in your organization is circulating URLs with upper-case letters in them.
- This probably won't impact SEO at all, since most services consider URL paths to be case-insensitive. If you're worried about duplicate content, a
<link rel="canonical"> tag pointing to the lower-case URL should solve that once and for all.
- There's a reason
_ci collations are the default – in particular, you usually expect case-insensitivity for any features relating to full-text search. Instead of changing the collation and dealing with the issues that arise from that, you could also talk to your SEO people about bike-shedding.