• Hello!

    On our website, the same page will load at /foo (the permalink) and /Foo (something someone types in by mistake).

    I thought WP would either reject /Foo to /foo (ideally), or 404 when attempting to access /Foo.

    Did this change recently? These are two different URLs.

    Is this behavior something that can be adjusted? We really don’t need capital letters in any of our permalinks or slugs, so really, they can be redirected to all lowercase.

    PS – I noticed this in our Google Analytics reports. And unfortunately, it doesn’t seem like this can be adjusted for as easily in the new version of GA(4).

    • This topic was modified 1 year, 11 months ago by xroox.
Viewing 9 replies - 1 through 9 (of 9 total)
  • By default, WordPress does not automatically redirect URLs with different capitalization to the corresponding lowercase URL. This means that if you have a page with a permalink of /foo, and someone tries to access /Foo, they will not be automatically redirected to the correct URL. Instead, they will see the page as it is with the incorrect URL.

    However, it is possible to adjust this behavior by using a plugin or modifying your website’s .htaccess file to redirect URLs with different capitalization to the correct lowercase URL.

    One option for doing this is to use a plugin like WP Force Lowercase URLs, which will automatically redirect all URLs with uppercase letters to their lowercase counterparts. This plugin can be installed and activated through the WordPress plugin repository.

    Alternatively, you can modify your website’s .htaccess file to redirect URLs with different capitalization to the correct lowercase URL. To do this, you can add the following code to your .htaccess file:

    
    RewriteEngine On
    RewriteMap lowercase int:tolower
    RewriteCond %{REQUEST_URI} [A-Z]
    RewriteRule (.*) ${lowercase:$1} [R=301,L]
    

    This code will redirect any URL with uppercase letters to the corresponding lowercase URL. It is important to note that modifying the .htaccess file can be a more advanced task and may require some technical knowledge. It is also important to make a backup of the file before making any changes, as any mistakes could potentially cause issues with your website.

    Regardless of which method you choose, it is a good idea to test the redirects to ensure that they are working as expected. You can do this by manually trying to access URLs with different capitalization and verifying that they are being redirected to the correct lowercase URL.

    • This reply was modified 1 year, 11 months ago by Mayank Kumar.

    Hi,
    Sorry for the trouble. By default, WordPress is case-insensitive when it comes to permalinks, so it will treat /foo and /Foo as the same page. This behavior cannot be changed in WordPress itself. However, you can use a plugin or modify your .htaccess file to redirect all capitalized permalinks to their lowercase counterparts.

    To do this with a plugin, you can use a plugin like “Force lowercase URLs” or “Lowercase URLs”. These plugins will automatically redirect all capitalized URLs to their lowercase counterparts.

    If you prefer to make the changes manually, you can add the following code to your .htaccess file:

    RewriteEngine On
    RewriteMap lowercase int:tolower
    RewriteCond %{REQUEST_URI} [A-Z]
    RewriteRule (.*) ${lowercase:$1} [R=301,L]

    This will redirect all capitalized URLs to their lowercase counterparts. It’s important to note that modifying the .htaccess file can have unintended consequences, so it’s always a good idea to back up your site before making any changes.

    In Google Analytics, you can use the “Case Sensitive” feature under the “Advanced” section of the “Secondary Dimension” dropdown to see how many hits each URL is receiving. This will allow you to see how many hits each case-insensitive variation of the URL is receiving.

    Thread Starter xroox

    (@xroox)

    Hi thanks for the replies.

    Did this change some time in the last few years? Because I am almost certain this used to just work out of the box. Maybe this code was part of WP and got removed…

    Thread Starter xroox

    (@xroox)

    BTW – these .htaccess code examples didn’t seem to affect anything. Where would I put this code in the file? I didn’t want to put it inside the WordPress section as the file explicitly says that anything between those comments will be deleted on updates.

    Thanks!

    @faisalahammad: I’m unable to test this now, but are you sure RewriteMap works in .htaccess?

    According to the Apache documentation, RewriteMap is only available in server config and virtual host contexts, so it shouldn’t work in .htaccess — going by what the documentation says.

    I finally got the chance to look into this, and, as the documentation says RewriteMap needs to be in the server config or virtual host context, it, therefore, doesn’t work in .htaccess. Depending on your server setup, this may do nothing, or even throw an error.

    If you have Apache’s mod_speling enabled, all you need in your .htaccess file is this:

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

    This article has another .htaccess method which loops over all the uppercase letters and rewrites to lowercase. The author says this is “really quick” (ie no performance hit), but I’ve not tested this myself.

    But all that’s for Apache, and if you’re running Nginx or some other web server software, none of the above will do you any good.

    For Nginx, you’re most likely going to have to install additional tools requiring root-level access.

    The plugins Faisal Ahammad mentioned worked for me though, so that’s definitely the easier path (though I personally prefer to do these sorts of rewrites at the webserver level).

    Good luck!

    Thread Starter xroox

    (@xroox)

    Thanks for sharing your findings, George!

    For thoroughness, do you know if it matters where in the .htaccess file this needs to go (i.e., before or after the WordPress-specific directives)?

    Hi @xroox
    You could insert it after the WordPress .htaccess rules. No worries.

    RewriteEngine On RewriteMap lc int:tolower RewriteCond %{REQUEST_URI} [A-Z] RewriteRule ^(.*)$ /${lc:$1} [R=301,L]

    This code sets up a rewrite map called “lc” that converts all uppercase characters to lowercase. Then, it checks if the requested URI contains any uppercase characters and performs a 301 redirect to the lowercase version of the URL.

    Make sure that the Apache mod_rewrite module is enabled for this to work. If you’re using a different web server, the process may vary slightly, but the concept remains the same.

    you can verify your urls on this redirect checker tool

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘URLs resolve w different capitalizations… Would like to redirect to all lower’ is closed to new replies.