有条件的.htaccess [英] Conditional .htaccess

查看:127
本文介绍了有条件的.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.

现在的问题是,我有一个多语种网站,所选择的语言是通过URL来控制。现在的网址是:

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

丹麦。这是所有工作得很好,但是我希望能够只使用了普通的URL为默认语言,例如 www.website.com/order 它会再只需选择默认语言(我在我的PHP code定义)。

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).

有没有简单的方法来做到这一点使用的.htaccess?现在我的.htaccess 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&page=$2 [nc]
RewriteRule ^(.*)$ index.php?page=$1 [nc]

但没有任何工程。

But nothing works..

推荐答案

这应该赶不以2字母语言code开始,并简单地重写所有那些做的所有网址,的index.php

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]

当然,你要确保你没有同时也是2个字母的组合...

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]

顺便说一句,你要永远记住使用QSA标志追加任何现有的GET paramters到重写URL。

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

这篇关于有条件的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆