.htaccess RewriteRule 帮助
我已经成功设置了一个重定向快捷方式的 URL 系统,如下所示:
# http://www.site.com/102/ -> http://www.site.com/102-some-keywords.html
RewriteRule ^([0-9]{3})/?$ includes/redirect.php?ref=$1 [L]
# http://www.site.com/102-some-keywords.html -> http://www.site.com/templates/default/index.php?ref=102
RewriteRule ^([0-9]{3})-.*?\.html$ templates/default/index.php?ref=$1 [L]
现在我正在升级到多语言系统,并希望在域之后获取语言代码:
# http://www.site.com/fr/102/ -> http://www.site.com/fr/102-some-keywords.html
# http://www.site.com/fr/102-some-keywords.html -> http://www.site.com/templates/default/index.php?loc=fr&ref=102
有些语言是 fr、it、es、ru、... 我想我需要多个变量 $1, $2 但我坚持这部分。
这是一个尝试(无法正常工作)
RewriteRule ^(.+)/(^[0-9]{3})/?$ includes/redirect.php?loc=$1&ref=$2 [L]
RewriteRule ^(.+)/(^[0-9]{3})-.*?\.html$ templates/default/index.php?loc=$1&ref=$2 [L]
I have successfully set up an URL system that redirects shortcuts like this:
# http://www.site.com/102/ -> http://www.site.com/102-some-keywords.html
RewriteRule ^([0-9]{3})/?$ includes/redirect.php?ref=$1 [L]
# http://www.site.com/102-some-keywords.html -> http://www.site.com/templates/default/index.php?ref=102
RewriteRule ^([0-9]{3})-.*?\.html$ templates/default/index.php?ref=$1 [L]
Now I am upgrading to a multi language system and want to get the language code right after the domain:
# http://www.site.com/fr/102/ -> http://www.site.com/fr/102-some-keywords.html
# http://www.site.com/fr/102-some-keywords.html -> http://www.site.com/templates/default/index.php?loc=fr&ref=102
some languages are fr, it, es, ru,...
I guess I need multiple variables $1, $2 but I am stuck with this part.
Here is a try (not working correctly)
RewriteRule ^(.+)/(^[0-9]{3})/?$ includes/redirect.php?loc=$1&ref=$2 [L]
RewriteRule ^(.+)/(^[0-9]{3})-.*?\.html$ templates/default/index.php?loc=$1&ref=$2 [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的新代码基本上是正确的,只是您忘记了
[0-9]
之前的^
(匹配表达式开始)。只需删除这些内容就可以使代码正常工作,但我个人会为国家/地区代码添加更严格的匹配:[az]{2}
Your new code is basically correct, except that you forgot the
^
(match start of expression) before[0-9]
. Just removing those should make the code work, but I'd personally add stricter matching for the country code:[a-z]{2}