htaccess重定向,如果url末尾的多重斜杠

时间:2013-11-29 15:03:42

标签: .htaccess mod-rewrite

由于SEO要求,我必须捕获所有这些类型的请求:

1 - http://example.com
2 - http://example.com////
3 - http://example.com////about
4 - http://example.com////about////
5 - http://example.com////about////info
6 - http://example.com////about////info////

并转发用户301重定向到

1 - http://example.com/
2 - http://example.com/
3 - http://example.com/about/
4 - http://example.com/about/
5 - http://example.com/about/info/
6 - http://example.com/about/info/

对于第一种情况,我使用

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ $1/ [L,R=301]

但是不能理解如何为其他案件编写规则

2 个答案:

答案 0 :(得分:1)

请尝试以下规则:

RewriteEngine on
RewriteBase /

# add trailing slash if missing
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302,NC,NE]

# remove multiple slashes
RewriteCond %{THE_REQUEST} \s/+(.*?)//(.*?)\s [NC]
RewriteRule ^ %1/%2 [L,R=302,NC,NE]

答案 1 :(得分:0)

# Remove multiple slashes anywhere in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
相关问题