Multilanguage site, subdirectories as language (RewriteRule)

强颜欢笑 提交于 2019-12-19 11:06:01

问题


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!


回答1:


Try this rule:

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


来源:https://stackoverflow.com/questions/3439668/multilanguage-site-subdirectories-as-language-rewriterule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!