多语言站点,子目录作为语言(RewriteRule)

发布于 2024-09-13 07:43:21 字数 1043 浏览 2 评论 0原文

对于我的网站,我想将我的网址重写为:

www.xxx.com/en/index.php 而不是 www.xxx.com/index.php?language=en
www.xxx.com/nl/index.php 而不是 www.xxx.com/index.php?language=nl

www.xxx.com 应该是 www.xxx.com/en/

这实际上有效我已经添加了这些重写规则

<代码> RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.xxx\.com(\/?)$
RewriteRule (.*) http://www.xxx.com/en/$1 [R=302,L]

RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]

添加 R=302 是为了测试目的

上面给出的 RewriteCond 和 RewriteRule 是否足够好,或者是否有其他可能更短或更好的方法将 .com/ 重写为 .com/en/

如果 url 被编辑为不支持的语言存在我希望该用户重定向到英语。

示例
请求 www.xxx.com/es/index.php,该语言不存在或未给出语言我希望用户重定向到 www.xxx.com/en/index.php

我尝试了以下操作:

<代码> RewriteRule ^([^n][^l]|[^e][^n])/(.*)$ en/$2%{QUERY_STRING} [L,R=302]

这在某些情况下有效,但如果输入 www.xxx.com/ne/index.php,则它被认为是有效的并且不会被重写。 www.xxx.com/index.php 也没有重写为 www.xxx.com/en/index.php

有人可以帮我解决这个问题吗?

提前致谢!

For my website i want to rewrite my url to:

www.xxx.com/en/index.php instead of www.xxx.com/index.php?language=en
www.xxx.com/nl/index.php instead of www.xxx.com/index.php?language=nl

www.xxx.com should be www.xxx.com/en/

This actually works i have added these rewrite rules


RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.xxx\.com(\/?)$
RewriteRule (.*) http://www.xxx.com/en/$1 [R=302,L]


RewriteRule ^(nl|en)/(.*)$ $2?language=$1&%{QUERY_STRING} [L]

The R=302 is added for testing purposes

Are the above given RewriteCond and RewriteRule good enough or are there other, maybe shorter or better ways to rewrite .com/ to .com/en/

If the url is edited to a language that does not exists i want this user to be redirect to english.

Example
www.xxx.com/es/index.php is requested, this language does not exists or no language is given i want the user to be redirect to www.xxx.com/en/index.php

I tried the following:


RewriteRule ^([^n][^l]|[^e][^n])/(.*)$ en/$2%{QUERY_STRING} [L,R=302]

This works in some cases but if www.xxx.com/ne/index.php is entered it is considered as valid and not rewritten. www.xxx.com/index.php is also not rewritten to www.xxx.com/en/index.php

Could someone help me to fix this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

苹果你个爱泡泡 2024-09-20 07:43:21

试试这个规则:

RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]

Try this rule:

RewriteCond $1 !^(en|nl)$
RewriteRule ^([a-z]{2})/(.*)$ en/$2 [L,R=302]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文