如何在 .htaccess 文件中编写 if - elseif - elseif 条件块?
这就是我想用伪代码完成的事情:
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
然而,这是我迄今为止实际拥有的,但似乎并没有像我预期的那样工作:
RewriteCond %{HTTP_HOST} =productionsite.com
RewriteBase /
RewriteCond %{HTTP_HOST} =stagingsite.com
RewriteBase /staging/mysite
RewriteCond %{HTTP_HOST} =localhost
RewriteBase /dev/mysite
我感谢我可以提前获得的任何帮助。
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
I appreciate any help I can get in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本身不是答案(作为评论开始),但可能对您有一点帮助:
您的方法不起作用,因为 RewriteBase 不受 RewriteCond 指令的影响。 RewriteCond 后面只能跟另一个 RewriteCond 或 RewriteRule。 RewriteBase 是一个完全不同的野兽:我不认为有条件的 RewriteBase 是可能的;我有一种模糊的感觉,它是在启动时处理的,所以不能动态更改(但我对此可能是非常错误的)。谷歌有很多关于“条件重写库”的点击,你可能会浏览这些,但它们似乎大多是负面的。
玩弄 IfDefine 来实现类似的效果值得一试。
或者,您可以尝试使用 RewriteRules 和环境变量来执行此操作:
然后在每个重写路径前添加 FRB 环境变量,例如
但这只是一个未经测试的理论。
Not an answer per-se (started out as a comment), but may help you a bit:
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.
Toying with IfDefine to achieve a similar effect would be worth a shot.
Alternatively, you could try doing it with RewriteRules and environmental variables:
And then prepend each rewrite path with the FRB environmental variable, like
But this is just an untested theory.
对我来说,解决方案来自 https://snipt.net/beat/htaccess - different-bases-subfolders-in-developmentproduct/ 有效
For me the solution from https://snipt.net/beat/htaccess-different-bases-subfolders-in-developmentproduction/ worked