.htaccess多语言网站

时间:2014-05-09 17:50:12

标签: .htaccess mod-rewrite

所以我想建立一个多语言网站 起初我考虑过通过cookie设置语言参数,但谷歌表示没有推荐。而且我不想惹谷歌,因为我的大多数访问者来自搜索引擎 谷歌建议子域名喜欢lang.example.com或像example.com/lang那样的目录 但是我不想在我的主机中创建多个子域和目录,因此我将在URL中使用lang参数,例如example.com/home?lang=de或example.com/about?lang=fr
当然,我希望它看起来像目录。我尝试了多种htaccess配置,但它要么根本不起作用,要么用多个参数搞乱我的其他重写规则。
我的老工作htaccess看起来像这样:

RewriteRule ^about/?$ ?page=about [NC,QSA]
RewriteRule ^home/?$ ?page=home [NC,QSA]
RewriteRule ^browse/?$ ?page=browse [NC,QSA]

RewriteRule ^contact/?$ ?page=about&what=contact [NC,QSA]
RewriteRule ^message/?$ ?page=about&what=send [NC,QSA]

RewriteRule ^browse/([^/]+)$ ?page=make&make=$1 [NC,L,QSA]

RewriteRule ^browse/(.+)/(.+)/?$ ?page=browser&make=$1&ser=$2 [NC,L,QSA]

我尝试添加

  

RewriteRule ^ de /(.*)$ / $ 1?lang = de_DE [L,QSA]

和类似的配置,但它永远不会如我所愿。

任何想法如何使其发挥作用?

1 个答案:

答案 0 :(得分:0)

你的规则如下:

RewriteEngine On

# handle URIs with language parameters like /en/, /it/, /de/, /fi/ etc
RewriteRule ^([a-z]{2})/(about|home|browse)/?$ ?page=$2&lang=$1 [NC,QSA,L]

RewriteRule ^([a-z]{2})/contact/?$ ?page=about&what=contact&lang=$1 [L,NC,QSA]
RewriteRule ^([a-z]{2})/message/?$ ?page=about&what=send&lang=$1 [L,NC,QSA]

RewriteRule ^([a-z]{2})/browse/([^/]+)/?$ ?page=make&make=$2&lang=$1 [NC,L,QSA]

RewriteRule ^([a-z]{2})/browse/([^/]+)/([^/]+)/?$ ?page=browser&make=$2&ser=$3&lang=$1 [NC,L,QSA]

# defaulting to english
RewriteRule ^(about|home|browse)/?$ ?page=$1&lang=en [NC,QSA,L]

RewriteRule ^contact/?$ ?page=about&what=contact&lang=en [L,NC,QSA]
RewriteRule ^message/?$ ?page=about&what=send&lang=en [L,NC,QSA]

RewriteRule ^browse/([^/]+)/?$ ?page=make&make=$1&lang=en [NC,L,QSA]

RewriteRule ^browse/([^/]+)/([^/]+)/?$ ?page=browser&make=$1&ser=$2&lang=en [NC,L,QSA]