B
B
bugsbunnyua2018-11-16 18:05:45
WordPress
bugsbunnyua, 2018-11-16 18:05:45

How to make a 301 redirect from uppercase to lowercase?

Hello. I made a redirect from uppercase to lowercase, everything worked, BUT the server response is still 200, how to make 301 ?
https://gyazo.com/fbe5ba20d5ae93fa72d398358ca198ae
I did everything by adding code to the index.php file

if ( $_SERVER['REQUEST_URI'] != strtolower( $_SERVER['REQUEST_URI']) ) {
    header('Location: //'.$_SERVER['HTTP_HOST'] . strtolower($_SERVER['REQUEST_URI']), true, 301);
    exit();
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
ljutaev, 2018-11-16
@bugsbunnyua

I hope it helps

function toLower() {
  // Получаем запрашиваемый URL
  $url = $_SERVER['REQUEST_URI'];
  $params = $_SERVER['QUERY_STRING'];
  // Если URL содержат имена файлов или имена файлов зависит от конкретики
   if ( preg_match('/[\.]/', $url) ) {
      return;
   }
  // Если URL содержит заглавную букву
      if ( preg_match('/[A-Z]/', $url) ) {
        // Преобразование URL в нижний регистр
        $lc_url = empty($params)
          ? strtolower($url)
          : strtolower(substr($url, 0, strrpos($url, '?'))).'?'.$params;

        // если URL был изменен, перенаправлять
        if ($lc_url !== $url) {

          // 301 redirect на новый URL нижнего регистра
          header('Location: '.$lc_url, TRUE, 301);
          exit();

        }

      }
   
  
}

V
Viktor Taran, 2018-11-17
@shambler81

I never understood why this should be done
in linu case-sensitive file system, not in windows.
There is a non-zero chance to upload the Image.jpg file and not get a response on the server with this scenario.
Have you seen by the metric how many 404s are on your site due to case?
What is the purpose of this movement?

B
bugsbunnyua, 2018-11-17
@bugsbunnyua

I hope it helps

And where to insert it? If in index.php it didn't help
I never understood why this should be done
in linu case-sensitive file system, not in windows.
There is a non-zero chance to upload the Image.jpg file and not get a response on the server with this scenario.
Have you seen by the metric how many 404s are on your site due to case?
What is the purpose of this movement?

SEOs sent an audit, the page gives a 200 response for two links with upper and lower case, that is, this is a duplicate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question