如何在 .htaccess 文件中编写 if - elseif - elseif 条件块?

发布于 2024-12-10 18:42:47 字数 575 浏览 0 评论 0原文

这就是我想用伪代码完成的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

套路撩心 2024-12-17 18:42:47

本身不是答案(作为评论开始),但可能对您有一点帮助:

您的方法不起作用,因为 RewriteBase 不受 RewriteCond 指令的影响。 RewriteCond 后面只能跟另一个 RewriteCond 或 RewriteRule。 RewriteBase 是一个完全不同的野兽:我不认为有条件的 RewriteBase 是可能的;我有一种模糊的感觉,它是在启动时处理的,所以不能动态更改(但我对此可能是非常错误的)。谷歌有很多关于“条件重写库”的点击,你可能会浏览这些,但它们似乎大多是负面的。

玩弄 IfDefine 来实现类似的效果值得一试。

或者,您可以尝试使用 RewriteRules 和环境变量来执行此操作:

 RewriteBase /

 RewriteCond %{HTTP_HOST} =stagingsite.com
 RewriteRule ^ - [E=FRB:/staging/mysite]

 RewriteCond %{HTTP_HOST} =localhost
 RewriteRule ^ - [E=FRB:/dev/mysite]

然后在每个重写路径前添加 FRB 环境变量,例如

 RewriteRule ^home$ %{ENV:FRB}/index.php

但这只是一个未经测试的理论。

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:

 RewriteBase /

 RewriteCond %{HTTP_HOST} =stagingsite.com
 RewriteRule ^ - [E=FRB:/staging/mysite]

 RewriteCond %{HTTP_HOST} =localhost
 RewriteRule ^ - [E=FRB:/dev/mysite]

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.

白云悠悠 2024-12-17 18:42:47

对我来说,解决方案来自 https://snipt.net/beat/htaccess - different-bases-subfolders-in-developmentproduct/ 有效

RewriteCond %{HTTP_HOST} !^localhost$
RewriteRule . - [E=REWRITEBASE:/prod/]

RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITEBASE:/dev/]

For me the solution from https://snipt.net/beat/htaccess-different-bases-subfolders-in-developmentproduction/ worked

RewriteCond %{HTTP_HOST} !^localhost$
RewriteRule . - [E=REWRITEBASE:/prod/]

RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule . - [E=REWRITEBASE:/dev/]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文