SetEnvIf HTTP_HOST 不起作用
我无法让它在 .htaccess 的这一部分工作,IfDefine
永远不会运行。我做错了什么,setenvif
mod已启用。
RewriteBase /
SetEnvIf HTTP_HOST ^localhost$ local
<IfDefine local>
RewriteBase /codeigniter-app/
</IfDefine>
SetEnvIf HTTP_HOST ^testing.alex.com$ testing
<IfDefine testing>
RewriteBase /app/
</IfDefine>
这是基于: 使用 mod_rewrite 我可以指定RewriteCond 中的 RewriteBase?
编辑: 还有其他方法可以完成上述任务吗?
I can't get it work this part of .htaccess, the IfDefine
never runs. What am I doing wrong, setenvif
mod is enabled.
RewriteBase /
SetEnvIf HTTP_HOST ^localhost$ local
<IfDefine local>
RewriteBase /codeigniter-app/
</IfDefine>
SetEnvIf HTTP_HOST ^testing.alex.com$ testing
<IfDefine testing>
RewriteBase /app/
</IfDefine>
This is based on: With mod_rewrite can I specify a RewriteBase within a RewriteCond?
Edit:
Any other way to accomplish the above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SetEnvIf 适用于请求标头。所以你想使用 HOST 而不是 HTTP_HOST。例如
SetEnvIf works on request headers. So you want to use HOST not HTTP_HOST. E.g.
SetEnvIf
设置一个环境变量。它没有定义供 Apache 使用的新常量。请参阅
SetEnvIf
手册并将其与
进行比较手册。SetEnvIf
sets an environment variable. It doesn't define a new constant for Apache to use.See the
SetEnvIf
manual and compare it to<IfDefine>
manual.我的回答是,如果 Apache 不允许您执行您在此处尝试执行的操作,但那是因为据我所知,您不需要执行此操作即可执行您真正需要执行的操作。
似乎您正在尝试创建一个在配置方面反映您的(远程)基于服务器的测试环境的本地环境。然而,您混淆了 URI 命名空间和文件系统命名空间,在 Apache Server 中,它们是完全不同的。例如,
指令对前者进行操作,
指令对后者进行操作。.htaccess
有点跨越两者,因此您需要了解参数何时对其中一个或另一个进行操作。例如,我有一个共享服务帐户,该帐户为许多子域
*.ellisons.org.uk
提供服务 - 例如我的博客位于 http://blog.ellisons.org.uk/。我在 Ubuntu 笔记本电脑和镜像我的服务提供商操作的 LAMP/suPHP 配置的虚拟机上进行开发(但我具有 root 访问权限,并且可以在虚拟机上查看重写日志等) 我在中静态分配 IP 地址192.168.1.0/24
私有地址空间并具有/etc/hosts
符号但除此之外,URI 空间是相同的,所以
blog.ellisons.org.XX/themes/terry/all.css
从我的服务器、笔记本电脑或虚拟机加载我的 css,具体取决于 XX=uk,home,vm但是我笔记本电脑上的文档根目录是 Debian标准
/var/www
,但在另外两个上它是/websites/LinuxPackage02/el/li/so/ellisons.org.uk/public_html
(我的托管商命名 习俗)。然而,我的应用程序(包括.htaccess
文件)是相同的,这些反映这种多次使用的唯一方法是在正则表达式中,我使用正则表达式来匹配 %{HTTP_HOST}(\w+)\ .ellisons.\org\.(?:uk|home|vm)
。以您的示例为例:
/app
的请求指向本地 PC 上的物理目录/codeigniter-app
。您可以在服务器或虚拟主机配置中使用Alias
指令来执行此操作。RewriteBase
采用 URI 路径参数,因此/app
可以在两个都。RewriteCond< /code> 字符串,您可以使用
%{DOCUMENT_ROOT}
——不过,如果您的服务是共享服务,您可能必须使用环境变量(我的是%{ENV:DOCUMENT_ROOT_REAL}
)。我 phpinfo() 会告诉你哪个。RewriteRule
替换中使用相对地址(无前导斜杠)。总而言之,只要稍加思考,您就可以使您的配置在逻辑上与 Apache 提供的相同——这不是比尝试以从未设计到产品中的方式搞砸条件执行更好吗?
My answer if that Apache doesn't allow you to do what you are trying to do here, but that's because AFAIK you don't need to do this to do what you really need to do.
It seems as if you are trying to create a local environment that in configuration terms mirrors your (remote) server-based test environment. However you are confusing the URI namespace and the file system namespace and in Apache Server these are quite different. For example the
<Location>
directive operates on the former and the<Directory>
directive operates on the latter..htaccess
sort of cut across both so you need to understand when a parameter operates on one or the other.For example, I have a shared service account which service a number of subdomains
*.ellisons.org.uk
-- for example my blog is on http://blog.ellisons.org.uk/. I develop on an Ubuntu laptop and a VM that mirrors the LAMP/suPHP configuration that my service provider operates (but I have root access and can see the rewrite logs, etc. on the VM) I statically allocate IP addresses in my192.168.1.0/24
private address space and have/etc/hosts
symonyms forBut other than this the URI space is identical,so
blog.ellisons.org.XX/themes/terry/all.css
loads my css from my server,laptop or VM depending on whether XX=uk,home,vmHowever the docroot on my laptop is Debian standard
/var/www
, but on the other two it is/websites/LinuxPackage02/el/li/so/ellisons.org.uk/public_html
(my hosters naming convention). Yet my application including.htaccess
files is identical, and the only way that these reflect this multiple use is in regexps where I using as a regexp to match %{HTTP_HOST}(\w+)\.ellisons.\org\.(?:uk|home|vm)
.So to your example:
/app
to your physical directory/codeigniter-app
on your local PC. You can use anAlias
directive in your server or vhost config to do this..RewriteBase
takes a URI path argument so/app
would work in both.RewriteCond
string, you can use%{DOCUMENT_ROOT}
-- though if your service is a shared service you may have to use an environment variable instead (mine is%{ENV:DOCUMENT_ROOT_REAL}
). I phpinfo() will tell you which.RewriteRule
substitutions (no leading slashes).So to summarise, with a little bit of thought you can make your configurations logically identical as Apache offers -- isn't that better than trying to botch in conditional execution in a way that was never designed into the product?
您还需要通过为虚拟主机配置添加以下内容来确保实际使用
.htaccess
文件:AllowOverride All
指令可以覆盖.htaccess
文件中的配置指令基于每个目录。You also need to make sure so that the
.htaccess
file is actually used by adding the following for your virtual host configuration:The
AllowOverride All
directive makes it possible to override configuration directives from an.htaccess
file on a per directory basis.