简体   繁体   中英

301 redirect from double slash to one slash in .htaccess file

I want to redirect some urls with 301 status code

Example:

from www.domain.com//brands/ to www.domain.com/brands/

from www.domain.com//brands/brand1 to www.domain.com/brands/brand1

I've tried this

Redirect 301 //brands/ /brands/

It mostly works. But on this url doesn't www.domain.com/brands/brand1 gets redirected to www.domain.com/brandsbrand1

try this rule,

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule / http://www.example.com/%1/%2 [R=301,L]

also you can try with RedirectMatch:

RedirectMatch 301 ^(.*)//+(.*)$ http://www.example.com/$1/$2

I solved with php.

I used following code:

if(strpos($_SERVER['REQUEST_URI'], "//") !== false || strpos($_SERVER['REQUEST_URI'], "///") !== false){
  $url = str_replace("///", "/", $_SERVER['REQUEST_URI']);
  $url = str_replace("//", "/", $url);
  $protocol = "http";
  if(isset($_SERVER['HTTPS'])){
   $protocol = "https";
  }
  $url_final = $protocol . "://" . $_SERVER['HTTP_HOST'] . $url;
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: $url_final");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM