E
E
Evanre2018-12-10 01:14:42
Nginx
Evanre, 2018-12-10 01:14:42

How to set up a 301 redirect from upper to lower case in WordPress?

Hi, an SEO did an audit of a simple WordPress site and one of the recommendations was to set up 301 redirects from uppercase to lowercase links. For example:
https://site.com/Post-slug > https://site.com/post-slug
I'm wondering how best to do this with minimal performance loss? Is this configured on the server side or in the wp php code?
Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly, 2018-12-10
@Evanre

If you don't want to bother with setting up htaccess, you can use the plugin: WP Force Lowercase URLs. I myself recently solved such a problem, I also thought at first without a plugin, so as not to create an extra load, then I opened the plugin code, and there are 60 lines (moreover, elementary).

spoiler
class WPForceLowercaseURLs {
    /**
     * Initialize plugin
     */
    public static function init() {
      // If page is non-admin, force lowercase URLs
      if ( !is_admin() ) {
        add_action( 'init', array('WPForceLowercaseURLs', 'toLower') );
      }
    }
    /**
     * Changes the requested URL to lowercase and redirects if modified
     */
    public static function toLower() {

      // Grab requested URL
      $url = $_SERVER['REQUEST_URI'];
      $params = $_SERVER['QUERY_STRING'];

      // If URL contains a period, halt (likely contains a filename and filenames are case specific)
      if ( preg_match('/[\.]/', $url) ) {
        return;
      }
      // If URL contains a capital letter
      if ( preg_match('/[A-Z]/', $url) ) {
        // Convert URL to lowercase
        $lc_url = empty($params)
          ? strtolower($url)
          : strtolower(substr($url, 0, strrpos($url, '?'))).'?'.$params;

        // if url was modified, re-direct
        if ($lc_url !== $url) {

          // 301 redirect to new lowercase URL
          header('Location: '.$lc_url, TRUE, 301);
          exit();
        }
      }
    }
  }
  WPForceLowercaseURLs::init();

I don't think it will load the site much. In general, I installed the plugin and did not take a steam bath.

O
Orkhan Hasanli, 2018-12-10
@azerphoenix

Useful article on your topic -
https://www.goinflow.com/redirect-uppercase-urls-t...
And here is the solution from this article -
https://brianflove.com/2014/08/11/lowercase-your -uris/ The
question is whether you have access to the httpd config file. If shared hosting, then most likely not

V
Viktor Taran, 2018-12-10
@shambler81

And once again I ask why this idiotic redirect?
1. In Windows, unlike Linux, the register has no meaning, but in Linux at the file system level it does.
So you will have to exclude all real directories and directories and links.
2. show the case with a screenshot when you see the entrance to such a page in the metric with an erroneous url register.
And then we will return to this idiotic redirect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question