dimayakovlev / getsimple-cms-extended

GetSimple CMS Extended
https://github.com/dimayakovlev/getsimple-cms-extended
GNU General Public License v3.0
1 stars 0 forks source link

Prevent GSCANONICAL redirect if page URL contains query string #5

Closed dimayakovlev closed 3 years ago

dimayakovlev commented 3 years ago

Return of function find_url() compared with $_SERVER['REQUEST_URI']. If requested URL contains query string client redirected to URL formed by function find_url().

dimayakovlev commented 3 years ago

Workaround is a compare if $_SERVER['REQUEST_URI'] starts with a result of find_url().

In PHP 8 added function str_starts_with(): https://www.php.net/manual/en/function.str-starts-with.php

For older version of PHP:

function str_starts_with($haystack, $needle) {
  return strpos($haystack , $needle) === 0;
}

This code can be used in index.php for correct comparison of requested and generated page urls:

if (getDef('GSCANONICAL', true)) {
  if (strpos($_SERVER['REQUEST_URI'], find_url($url, $parent, 'relative')) !== 0) {
    redirect(find_url($url, $parent));
  }
}