Javascript is off

How to make a 301 redirect on a site with www or without

Author: Aren Proger 09-16-2020

254 views

Now the problem with SEO redirects 301 is difficult to find on the Internet, but here is the final solution

When the site is ready, it is necessary to make all redirection on one url of each page on php, 

Laravel and on any php framework

By prescribing this simple code, you can easily redirect your site to https and with or without www

// We get the page url

$thisurl = $_SERVER["REQUEST_SCHEME"] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

// canonical means what the url of this page should be,  if it does not coincide with $ thisurl we make redirection to $ canonical url

$canonical = 'https://www.yourdomain.com' . $_SERVER['REQUEST_URI'];

if (($thisurl != $canonical)) {

     header("HTTP/1.1 301 Moved Permanently");

     header("Location: $canonical");

     exit();

}