mod_rewrite将'_'替换为'-' [英] mod_rewrite replace '_' with '-'
问题描述
我几乎已经有了mod_rewrite规则,但是我已经陷进了:)
I'm almost there with a mod_rewrite rule, but I've caved in :)
我需要重写
country/[countryname].php
到
country/[countryname]/
但是,[countryname]可能带有这样的下划线:'south_africa.php',如果确实如此,我想将其替换为连字符:'south-africa/'
however, [countryname] may have an underscore like this: 'south_africa.php' and if it does I want to replace it with a hypen: 'south-africa/'
如果该国家/地区后面有数字,我也想匹配:从'france03.php'到'france/'
I also want to match if the country has numbers following it: 'france03.php' to 'france/'
这里是我的规则,几乎在这里,但是即使下划线后没有第二部分,它仍然会添加连字符.
Heres my rule, its almost there but its still adding a hyphen even if there is no second part after the underscore.
RewriteRule ^country/(.*)_(.*?)[0-9]*\.php$ country/$1-$2 [R=301,L]
因此当前"country/south_.php"变为"country/south-/"
so currently 'country/south_.php' becomes 'country/south-/'
有人可以帮我找到难题的缺失部分吗?谢谢.
Can someone please help me find the missing piece of the puzzle? Thanks.
推荐答案
尝试一下:
RewriteRule ^country/([^_]*)_([^_]*?)\d*\.php$ country/$1-$2 [R=301,L]
此规则将使用带有单个下划线的网址进行匹配-您需要使用其他规则来获得更多的下划线,否则就不使用.
This rule will match urls with a single underscore - you'll need a different rule for more underscores or none.
如果要确保$2
仅包含字母并且不为空,请将([^_]*?)
更改为([a-zA-Z]+)
.
If you want to make sure $2
contains only letter and isn't empty, change ([^_]*?)
it to ([a-zA-Z]+)
.
这篇关于mod_rewrite将'_'替换为'-'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!