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

时间:2010-08-09 11:48:56

标签: .htaccess mod-rewrite

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

  

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]

RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.xxx\.com(\/?)$

添加R = 302用于测试目的

以上给出的RewriteCond和RewriteRule是否足够好,还是有其他的,可能更短或更好的方法来重写.com / to .com / en /

如果将网址编辑为不存在的语言,我希望将此用户重定向到英语。

实施例
请求www.xxx.com/es/index.php,此语言不存在或未提供任何语言我希望用户重定向到www.xxx.com/en/index.php

我尝试了以下内容:

RewriteRule (.*) http://www.xxx.com/en/$1 [R=302,L]

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

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

提前致谢!

1 个答案:

答案 0 :(得分:2)

尝试此规则:

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