简体   繁体   English

301从.htaccess文件中的双斜杠重定向到一个斜杠

[英]301 redirect from double slash to one slash in .htaccess file

I want to redirect some urls with 301 status code 我想使用301状态代码重定向某些网址

Example: 例:

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

from www.domain.com//brands/brand1 to www.domain.com/brands/brand1 从www.domain.com//brands/brand1到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 但在此网址不www.domain.com/brands/brand1被重定向到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:

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

I solved with php. 我用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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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