删除多个尾部斜杠mod

编程入门 行业动态 更新时间:2024-10-14 20:19:07
本文介绍了删除多个尾部斜杠mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道仅在此站点上就曾多次问过这个问题,但是在浏览相关帖子时我找不到解决方案.尝试删除域后的多个尾部斜杠.以下mod_rewrite表达式似乎适用于诸如 www之类的URL. domain//path1///path2////,但不适用于domain//

I know this question was asked a number of times on this site alone, but browsing through the relevant posts I couldn't find a solution. Trying to remove multiple trailing slashes after domain. The following mod_rewrite expressions seem to work for URLs such as www.domain//path1///path2////, but do not work for domain//

DirectorySlash Off RewriteEngine on # Canonical fix RewriteCond %{HTTP_HOST} !^www.domain$ [NC] RewriteRule ^(.*)$ www.domain/$1 [R=301] RewriteRule ^/main.do www.domain/ [R=301,L] RewriteRule ^/index.jsp www.domain/ [R=301,L] # Remove bogus query strings RewriteCond %{query_string} q= [NC] RewriteRule (.*) www.domain/$1? [R=301,L] # Remove multiple slashes after domain - DOESN'T WORK!!! #RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR] #RewriteCond %{REQUEST_URI} ^(.*/)/+$ #RewriteRule / www.domain/%1 [R=301,L] # Remove multiple slashes anywhere in URL RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ RewriteRule . %1/%2 [R=301,L] # Externally redirect to get rid of trailing slash except for home page, ads RewriteCond %{REQUEST_URI} !^/ads/ RewriteRule ^(.+)/$ $1 [R=301,L]

推荐答案

无法复制.即使关闭DirectorySlashes,域之后也不会将多余的斜杠传递给mod_rewrite -我尚未检查是删除了斜杠还是Opera还是Apache.但是否则,一切都会正常进行:

Can't reproduce. Extra slashes immediately after the domain are never passed to mod_rewrite even with DirectorySlashes off -- I haven't checked whether it's Opera or Apache that's removing the slash). But otherwise, everything works fine:

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

请求 localhost//abc/b//

127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (3) [perdir C:/HTTP/htdocs/] add path info postfix: C:/HTTP/htdocs/abc -> C:/HTTP/htdocs/abc/b/ 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (3) [perdir C:/HTTP/htdocs/] strip per-dir prefix: C:/HTTP/htdocs/abc/b/ -> abc/b/ 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (3) [perdir C:/HTTP/htdocs/] applying pattern '.*' to uri 'abc/b/' 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (4) [perdir C:/HTTP/htdocs/] RewriteCond: input='/abc//b//' pattern='^//+(.*)$' => not-matched 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (4) [perdir C:/HTTP/htdocs/] RewriteCond: input='/abc//b//' pattern='^(.*/)/+$' => matched 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (2) [perdir C:/HTTP/htdocs/] rewrite 'abc/b/' -> 'domain//abc//b/' 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (2) [perdir C:/HTTP/htdocs/] explicitly forcing redirect with domain//abc//b/ 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (1) [perdir C:/HTTP/htdocs/] escaping domain//abc//b/ for redirect 127.0.0.1 - - [17/May/2010:12:26:36 +0100] [localhost/sid#c08fb0][rid#2756540/initial] (1) [perdir C:/HTTP/htdocs/] redirect to domain//abc//b/ [REDIRECT/301]

注意:请考虑不要对主机进行硬编码:

Note: consider not hard-coding the host:

RewriteCond %{HTTP_HOST} !="" RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR] RewriteCond %{REQUEST_URI} ^(.*/)/+$ RewriteRule .* %{HTTP_HOST}/%1 [R=301,L]

还要注意,内部的"//"没有被替换.您将添加另一个规则来替换内部斜杠.

Also note that the inner "//" was not replaced. You will to add another rule to replace inner slashes.

新 好的,这似乎可以防止以//::

NEW OK, this seems to work for preventing URLs starting or ending with //:

RewriteEngine on RewriteBase / RewriteCond %{HTTP_HOST} !="" RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR] RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$ RewriteRule .* %{HTTP_HOST}/%1 [R=301,L]

更多推荐

删除多个尾部斜杠mod

本文发布于:2023-11-01 13:15:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   多个   尾部   mod

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!