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

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

我有以下网址:

example.com/hellllllllllo

我一直在寻找一种方法来避免重复字符最多加倍。

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

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

最佳答案

这是我的完整答案,按照 samurai8 在之前评论中的建议,使用惰性匹配来避免 url 中的重复字符:

对于重复的斜杠和破折号

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]
.
.
.

关于.htaccess - 如何从 url 中删除重复的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19439437/

相关文章:

.htaccess - 如何使用 htaccess 重写 GET 参数

asp.net-mvc-3 - 将图像放在 CDN 上,在 IIS7 上使用 MVC3

.htaccess - 如何使用 htaccess 重定向一堆文件

php - Magento 站点切换.htaccess

apache - htaccess HSTS 阻止从 www.subdomain.domain.com 重定向到 subdomain.domain.com

url - 如何在Grails中将URL重定向到特定的Controller/Action?

php - 使用 PHP 下载文件并将其保存在本地

regex - 在 bash 中解码 URL

php - 用 PHP 重写 URL

apache - mod_rewrite : remove query string from URL?