.htaccess - 如何从网址中删除重复的字符?

时间:2013-10-18 00:10:50

标签: .htaccess url url-rewriting repeat duplicate-removal

我有以下网址:

example.com/hellllllllllo

我正在寻找一种避免重复角色加倍的方法。

受此问题的启发/答案Remove Characters from URL with htaccess我创建了以下htaccess文档以避免重复的字符。如果角色重复超过23次,则网址没有完全重写,我想知道是否有任何可能的改进?

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

1 个答案:

答案 0 :(得分:5)

以下是我的完整答案,以避免在之前的评论中使用samurai8建议的使用延迟匹配的网址中重复的字符:

重复轻松和潇洒

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

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

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

重复字母

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

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

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