【问题标题】:Issue In Removing Double Or More Slashes From URL By .htaccess通过 .htaccess 从 URL 中删除双或更多斜杠的问题
【发布时间】:2013-06-09 11:02:13
【问题描述】:

我正在使用以下 htaccess 规则从 web url 中删除双斜杠或多个斜杠:

#remove double/more slashes in url
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

这对于出现在 uri 中间的斜线效果很好,例如,如果使用 url:

http://demo.codesamplez.com/html5//audio

它被重定向到正确的单斜杠网址:

http://demo.codesamplez.com/html5/audio

但是如果网址开头包含双斜杠,就在域名之后,那么它就不起作用,例如:

http://demo.codesamplez.com//html5/audio

它没有被重定向。

我怎样才能修复上述规则以适用于这种类型的网址?谢谢。

【问题讨论】:

    标签: .htaccess url slash


    【解决方案1】:

    试试看:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
    RewriteRule ^(.*) $1 [R=301,L]
    

    它应该重定向到域末尾的单个斜杠。 以及对您的改进:

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

    【讨论】:

    • 根据您的回答,为了替换多个连字符,我做了: RewriteCond %{REQUEST_URI} ^(.*)--(.*)$ RewriteRule 。 %1-%2 [R=301,L] -- 有效,谢谢
    • @Marcel 似乎没有在 url 中保留查询参数。
    【解决方案2】:

    为了防止您的网址中出现长时间重复的字符,例如:

    http://demo.codesamplez.com/html5///////////////////////////////////////////audio
    

    你可以这样做:

    RewriteCond %{REQUEST_METHOD}  !=POST
    RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
    RewriteRule . %1/%3 [R=301,L]
    

    它应该适用于:

    http://demo.codesamplez.com//html5/audio
    

    另请参阅:.htaccess - how to remove repeated characters from url?

    【讨论】:

    • 如果 url 是 domain.com 将不起作用// - 重定向的 url 将是相同的!
    • 这个答案已经足够好了,并且适用于给定的例子。在您的情况下,使用 RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)?$ 将最后一组字符声明为可选。下次投反对票前请先询问。
    【解决方案3】:

    对我来说,以下规则非常有效:

    <IfModule mod_rewrite.c>
        RewriteBase /
    
        # rule 1: remove multiple leading slashes (directly after the TLD)
        RewriteCond %{THE_REQUEST} \s/{2,}
        RewriteRule (.*) $1 [R=301,L]
    
        # rule 2: remove multiple slashes in the requested path
        RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
        RewriteRule (.*) %1/%2 [R=301,L]
    </IfModule>
    

    这个想法很大程度上基于 Marcels 的回答(谢谢!),但这个想法更轻量级,包括 RewriteBase,如果您使用特定的子目录结构,这可能会有所帮助。此外,Marcels 的回答缺乏解释,我想解决这个问题:

    规则 1:{THE_REQUEST} 包含类似GET /index.html HTTP/1.1 的内容(请参阅docs)。因此,如果我们匹配第一个空格 (\s) 后跟多个斜杠 (/{2,}),我们可以通过 $1 访问没有前导双斜杠的正确 URL。

    规则 2:正则表达式 ^(.*)/{2,}(.*)$ 将请求 URI 拆分为多个斜杠。 %1/%2 然后再次组合两个拆分的字符串,但此时只有一个斜线。

    【讨论】:

    • 但假设如下:localhost/hey/thanks/a/lot//////////被执行,结果是localhost/hey/thanks/a/lot/我想删除最后的斜线
    • @AlanDeep 这是一个不同的问题,使用不同的solution
    • 这似乎无法处理在 URL 中多次出现多个斜杠的更复杂的情况 - 例如///some//more///complicated///path//
    【解决方案4】:

    根据this 链接,以下代码应注意 URL 中的额外斜杠(任何位置)。

    RewriteCond %{THE_REQUEST} //
    RewriteRule ^.*$ $0 [R=302,L,NE]
    

    【讨论】:

    • 如果在查询字符串的任何地方出现多个斜杠,正则表达式// 将创建一个重定向循环(与THE_REQUEST 相比)。链接的答案已更新为使用 \s[^?]*// - 这确保多个斜杠仅在 URL 路径中匹配,而不是查询字符串(如果存在)。
    【解决方案5】:

    只需将以下代码放入您的 .htaccess 文件即可。 它将从任何地方删除多个斜线。在 url 的末尾和从 url 的中间。

    <IfModule mod_rewrite.c>
    RewriteBase /`enter code here`
    RewriteCond %{THE_REQUEST} \s[^?]*//
    RewriteRule ^.*$ /$0 [R=302,L,NE]
    #Remove slash anywhere from url
    RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
    RewriteRule . %1/%2 [R=301,L]
    # Rule 1: remove multiple leading slashes (directly after the TLD)
    RewriteCond %{THE_REQUEST} \s/{2,}
    RewriteRule (.*) $1 [R=301,L]
    # Rule 2: remove multiple slashes in the requested path`enter code here`
    RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
    RewriteRule (.*) %1/%2 [R=301,L]
    </IfModule>
    

    【讨论】:

      【解决方案6】:

      使用美味的 .htaccess 片段很容易解决这种情况。您需要做的就是复制以下代码和您网站的根 .htaccess 文件:

      <IfModule mod_rewrite.c>
      RewriteBase /
      # Rule 1: remove multiple leading slashes (directly after the TLD)
      RewriteCond %{THE_REQUEST} \s/{2,}
      RewriteRule (.*) $1 [R=301,L]
      # Rule 2: remove multiple slashes in the requested path
      RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
      RewriteRule (.*) %1/%2 [R=301,L]
      </IfModule>
      

      规则 1: {THE_REQUEST} 包含类似 GET /index.html HTTP/1.1 的内容

      因此,如果我们匹配第一个空格 (\s) 后跟多个斜杠 (/{2,}),我们可以通过 $1 访问不带前导双斜杠的正确 URL。

      规则 2: 正则表达式 ^(.*)/{2,}(.*)$ 将请求 URI 拆分为多个斜杠。 %1/%2 然后再次组合两个拆分的字符串,但此时只有一个斜线。

      例如,这个指令将重定向如下:

      https://www.meysmahdavi.com// 重定向到 https://www.meysmahdavi.com/ https://www.meysmahdavi.com//blog-post/ 重定向到 https://www.meysmahdavi.com/blog-post/ https://www.meysmahdavi.com//path/directory/ 重定向到 https://www.meysmahdavi.com/path/directory/

      所以基本上它会从任何 URL 中删除双斜杠。

      来源:https://www.meysmahdavi.com/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-26
        • 2017-01-12
        • 2013-04-14
        • 1970-01-01
        • 2015-05-09
        • 2015-03-26
        • 1970-01-01
        相关资源
        最近更新 更多