.htaccess - 如何从 url 中删除重复的字符? [英] .htaccess - how to remove repeated characters from url?

查看:13
本文介绍了.htaccess - 如何从 url 中删除重复的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址:

example.com/hellllllllllo

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

And I was looking for a way to avoid repeated characters up to double.

受此问题/答案的启发使用 htaccess 从 URL 中删除字符 我已创建以下 htaccess 文档以避免重复字符.如果字符重复超过 23 次,则 url 没有完全重写,我想知道是否有任何可能的改进?

Inspired by this question/answers Remove Characters from URL with htaccess I have created the following htaccess document to avoid repeated characters. If the character is repeated more than 23 times the url is not completely rewrited and I was wondering if there is any possible improvment?

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

推荐答案

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

Here is my full answer to avoid repeated characters in urls using lazy match as suggested by samurai8 in previous comments:

用于重复的斜线和破折号

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 中删除重复的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆