简体   繁体   English

如果在htaccess中获取301,则将URL中的特殊字符重定向到其他字符?

[英]If in htaccess for 301 Redirect special characters anywhere in URL to another characters?

Rule: Redirecting all URLs that contains %252C+ to ,- and redirecting + to - only if the + comes with %252C+ . 规则:仅当+带有%252C+才将包含%252C+所有URL重定向到,- ,并将+重定向到-

e.g1: http://www.example.com/FortWorth%252C+TX/ to: http://www.example.com/FortWorth,-TX/ 例如: http://www.example.com/FortWorth%252C+TX/http://www.example.com/FortWorth%252C+TX/到: http://www.example.com/FortWorth,-TX/ : http://www.example.com/FortWorth,-TX/

e.g2: http://www.example.com/Fort+Worth%252C+TX/ to: http://www.example.com/Fort-Worth,-TX/ 例如:2: http://www.example.com/Fort-Worth,-TX/http://www.example.com/Fort+Worth%252C+TX/到: http://www.example.com/Fort-Worth,-TX/ : http://www.example.com/Fort-Worth,-TX/

e.g3: http://www.example.com/Fort+Worth/ to: http://www.example.com/Fort+Worth/ (Should not redirect this one) 例如:3: http://www.example.com/Fort+Worth/ : http://www.example.com/Fort+Worth/到: http://www.example.com/Fort+Worth/ : http://www.example.com/Fort+Worth/ (请勿重定向此地址)

Note: Please remember your code should not be only for the above URL but for all URLs with the above rule. 注意:请记住,您的代码不仅应用于上述URL,而且还应用于具有上述规则的所有URL。

The first argument of RewriteRule is matched against the %-decoded url, so it should be matched against http://www.example.com/FortWorth%2C+TX/ for example instead of http://www.example.com/FortWorth%252C+TX/ . RewriteRule的第一个参数与%解码的url匹配,因此应与http://www.example.com/FortWorth%2C+TX/匹配,例如,而不是http://www.example.com/FortWorth%252C+TX/

Assuming the strange substring only occurs once, and the "+" only appears once before that, you can do it with the following two rules. 假设奇怪的子字符串仅出现一次,而“ +”在此之前仅出现一次,则可以使用以下两个规则来实现。 If multiple +'s can occur, things start to turn ugly quickly, because you'll need to either add a lot more rules, or start handling it recursively. 如果可能出现多个+,则事情开始变得很难看,因为您需要添加更多规则,或者开始递归处理它。

RewriteRule ^([^+]+)\+(.+)%2C\+(.*)$ $1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C\+(.*)$ $1,-$2 [R,L]

I am not sure if mod_rewrite will escape the , character as a "special character". 我不知道如果mod_rewrite的将逃跑的,字符作为“特殊字符”。 It is a valid character to use in an url. 这是在网址中使用的有效字符。 If it gives you problems, you could use the [NE] flag to prevent escaping such characters. 如果给您带来问题,则可以使用[NE]标志来防止转义此类字符。

Thanks Sumurai8 , for your solution but it needs to be modified like This code : 感谢Sumurai8为您提供的解决方案,但需要像下面的代码一样进行修改:

RewriteEngine On

RewriteRule ^([^+]+)\+(.+)%2C+\+(.*)$ /$1-$2,-$3 [R,L]
RewriteRule ^([^+]+)%2C+\+(.*)$ /$1,-$2 [R,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM