开发者

Conditional .htaccess

I'm sure there are other threads on similar topics around the web but I just can't get this working on my own website.

The problem is that I have a multi-language website where the selected language is controlled through the URL. Right now the url is:

www.website.com/en/order

or whatever for English or

www.website.com/da/order

for Danish. This is all working just fine however I want to be able to use just the plain URL for the default language, for instance www.website.com/order where it'll then just select the default language (which I have defined in my php code).

Is there any easy way to do this using .htaccess? Right now my .htaccess code is as follows:

RewriteRule ^(.*)\/(.*)$ index.php?lang=$1&page=$2 [nc]

I've also tried having two rules:

RewriteRule ^(.*)\/(.*)$ index.php?lang=$1&am开发者_开发知识库p;page=$2 [nc]
RewriteRule ^(.*)$ index.php?page=$1 [nc]

But nothing works..


This one should catch all URLs that do not start with a 2-letter language code, and simply rewrite all those that do, to index.php

RewriteCond %{REQUEST_URI} !^/[a-z]{2}/ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

# match those that DO have a language code
RewriteRule ^([a-z]{2})/(.*)$ index.php?lang=$1&page=$2 [QSA,L,NC]

Of course, you want to make sure you don't have any page URLs that are also 2-letter combinations...

If you only have a small set of languages to support (e.g. "da", "en", "de"), you could make it more robust by using this instead

RewriteCond %{REQUEST_URI} !^/(da|en|de)/ [NC]

By the way, you'll want to always remember to use the QSA-flag to append any existing GET paramters to the rewritten URL.


You can try using a RewriteCond. For (an untested and probably wrong) example, using English as the default:

RewriteCond %{REQUEST_URI} ^\/..\/ [NC]
RewriteRule ^\/(..)\/(.*) index.php?lang=$1&page=$2 [NC]
RewriteCond %{REQUEST_URI} ^\/.* [NC]
RewriteRule ^\/(.*) index.php?lang=en&page=$1 [NC]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜