在.htaccess文件elseif的条件块 - 我怎么会写一个if - elseif的? [英] How would I write an if - elseif - elseif conditional block in an .htaccess file?
问题描述
这就是我要完成的伪code:
This is what I want to accomplish in pseudocode:
if server.host equals 'productionsite.com' then
RewriteBase /
else if server.host equals 'stagingsite.com' then
RewriteBase /staging/mysite
else if server.host equals 'localhost' or server.host equals '127.0.0.1' then
Rewrite /dev/mysite
不过,这是我居然有这么远,这似乎并没有工作,我期望:
However, this is what I actually have so far which doesn't seem to work as I expect:
RewriteCond %{HTTP_HOST} =productionsite.com
RewriteBase /
RewriteCond %{HTTP_HOST} =stagingsite.com
RewriteBase /staging/mysite
RewriteCond %{HTTP_HOST} =localhost
RewriteBase /dev/mysite
我AP preciate任何帮助,我可以提前得到。
I appreciate any help I can get in advance.
推荐答案
不是一个每本身答案(一开始是注释),但可能你有点帮助:
Not an answer per-se (started out as a comment), but may help you a bit:
您的方法不起作用,因为的RewriteBase不受的RewriteCond指令。一个的RewriteCond只能跟着另一个的RewriteCond或重写规则。的RewriteBase是一个完全不同的野兽:我不认为有条件的RewriteBase是可能的;我有模糊的感觉,它在启动时进行处理,所以不能动态地改变(但我可能是非常错误的这个)。谷歌有许多命中为有条件的RewriteBase,你可以看通过这些,但他们似乎主要是负面的。
Your approach doesn't work because the RewriteBase isn't affected by RewriteCond directives. A RewriteCond can only be followed by another RewriteCond or a RewriteRule. RewriteBase is an entirely different beast: I don't think that a conditional RewriteBase is possible; I have the vague feeling that it's processed at startup, so can't be changed dynamically (but I may be very wrong about this). Google has a number of hits for "conditional rewritebase", you may look through those, but they seem largely negative.
与 IfDefine 的玩弄以达到类似的效果将是值得一试。
Toying with IfDefine to achieve a similar effect would be worth a shot.
另外,你可以尝试使用规则集和环境变量这样做:
Alternatively, you could try doing it with RewriteRules and environmental variables:
RewriteBase /
RewriteCond %{HTTP_HOST} =stagingsite.com
RewriteRule ^ - [E=FRB:/staging/mysite]
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^ - [E=FRB:/dev/mysite]
然后prePEND每个重写路径与FRB环境变量,如
And then prepend each rewrite path with the FRB environmental variable, like
RewriteRule ^home$ %{ENV:FRB}/index.php
但是,这仅仅是一个未经测试的理论。
But this is just an untested theory.
这篇关于在.htaccess文件elseif的条件块 - 我怎么会写一个if - elseif的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!