.htaccess RewriteRule 帮助

发布于 2024-10-25 22:27:44 字数 920 浏览 1 评论 0原文

我已经成功设置了一个重定向快捷方式的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙与沫 2024-11-01 22:27:44

您的新代码基本上是正确的,只是您忘记了 [0-9] 之前的 ^ (匹配表达式开始)。只需删除这些内容就可以使代码正常工作,但我个人会为国家/地区代码添加更严格的匹配:[az]{2}

RewriteRule ^([a-z]{2})/([0-9]{3})/?$ includes/redirect.php?loc=$1&ref=$2 [L]
RewriteRule ^([a-z]{2})/([0-9]{3})-.*?\.html$ templates/default/index.php?loc=$1&ref=$2 [L]

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}

RewriteRule ^([a-z]{2})/([0-9]{3})/?$ includes/redirect.php?loc=$1&ref=$2 [L]
RewriteRule ^([a-z]{2})/([0-9]{3})-.*?\.html$ templates/default/index.php?loc=$1&ref=$2 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文