apache 我怎样才能有条件的 .htaccess 块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/736524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Can I Have A Conditional .htaccess Block?
提问by
This is an Apache question you've probably come across before. I want to have one source package that I can deploy to my workstation, my staging server, and my production server, but for it to load different .htaccess settings based on what the URL was.
这是您以前可能遇到过的 Apache 问题。我想要一个源包,我可以将它部署到我的工作站、我的临时服务器和我的生产服务器,但是它可以根据 URL 加载不同的 .htaccess 设置。
Note that I was using a kludge with an IfModule call, but that won't work with our new production server because it shares all the same modules as my staging server.
请注意,我使用了带有 IfModule 调用的 kludge,但这不适用于我们的新生产服务器,因为它与我的登台服务器共享所有相同的模块。
Note I need to bundle SetEnv with these rewrites. Currently if you use RewriteCond, it only ties to the following RewriteRule, but not the SetEnv underneath.
注意我需要将 SetEnv 与这些重写捆绑在一起。目前,如果您使用 RewriteCond,它仅与以下 RewriteRule 相关联,而不与下面的 SetEnv 相关联。
采纳答案by Michael Cramer
Instead of using SetEnv, use the environment variable setting capabilities of RewriteRule itself:
不使用 SetEnv,而是使用 RewriteRule 本身的环境变量设置功能:
RewriteCond %{HTTP_HOST} =foo.com
RewriteRule ^ - [E=VARNAME:foo]
RewriteCond %{HTTP_HOST} =bar.com
RewriteRule ^ - [E=VARNAME:bar]
Although I prefer doing this sort of thing by passing flags to the httpd process at startup and looking for them using IfDefine blocks.
虽然我更喜欢通过在启动时将标志传递给 httpd 进程并使用 IfDefine 块查找它们来做这种事情。
<IfDefine FOO>
SetEnv VARNAME foo
</IfDefine>
<IfDefine BAR>
SetEnv VARNAME bar
</IfDefine>
回答by Volomike
On Ubuntu Linux, the IfDefine's variable is set in
在 Ubuntu Linux 上,IfDefine的变量设置在
/etc/apache2/envvars
and is called APACHE_ARGUMENTS. So, at the bottom of that file I had to add:
并被称为APACHE_ARGUMENTS。因此,在该文件的底部,我必须添加:
export APACHE_ARGUMENTS="-D dev"
...and then bounce the server with:
...然后使用以下命令反弹服务器:
/etc/init.d/apache2 stop
/etc/init.d/apache2 start
On other systems:
在其他系统上:
However, there's a Debian article on this topic that discusses this here. In that example, the file to edit is /etc/default/apache2 and the variable is called APACHE_DEFINES.
然而,有关于这一主题,讨论这个Debian的文章在这里。在该示例中,要编辑的文件是 /etc/default/apache2 并且变量名为APACHE_DEFINES.
Likewise, on some systems it is a variable named OPTIONSthat is set in /etc/sysconfig/httpd.
同样,在某些系统上,它是一个名为的变量OPTIONS,设置在/etc/sysconfig/httpd.
So, what you really need to do is look for the start section in your apache2ctl file. So, begin by doing a whereis apache2ctlto find where that script is, cat it out and find the start section with the apache2 directive in it, and see if the variable it passes is OPTIONS, APACHE_ARGUMENTS, APACHE_DEFINES, or something else. Then, see which file you need to edit by experimentation with either /etc/sysconfig/httpd, /etc/default/apache2, or /etc/apache2/envvars.
因此,您真正需要做的是在 apache2ctl 文件中查找 start 部分。所以,首先做一个whereis apache2ctl找到此脚本,猫出来,并找到在它的Apache2指令开始部分,它是否能通过变量是OPTIONS,APACHE_ARGUMENTS,APACHE_DEFINES,或别的东西。然后,看你需要哪些文件进行编辑通过实验用两种/etc/sysconfig/httpd,/etc/default/apache2或/etc/apache2/envvars。
回答by Volomike
Here is a simple example that should be enough for you to change it to meet your requirements:
这是一个简单的示例,应该足以让您对其进行更改以满足您的要求:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^localhost
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/ [R=301,L]
</IfModule>
回答by chichilatte
I tried the IfDefinemethod and it didn't work, even though the defined variable i'd passed into the Apache startup was definitely showing up in phpinfo()on the APACHE_ARGUMENTSline.
我尝试了IfDefine方法,它没有工作,即使定义的变量我会传递到Apache的启动肯定是显示了phpinfo()上APACHE_ARGUMENTS线。
I tried another method and it worked perfectly. In your .htaccessfile you need something like:
我尝试了另一种方法,效果很好。在您的.htaccess文件中,您需要类似的内容:
# Is the domain local (i wanted to check two names)?
<If "%{SERVER_NAME} != 'localhost' && %{SERVER_NAME} != 'myproject.localhost'">
# I wanted to password protect the production server only
AuthType Basic
AuthName "Restricted area"
AuthUserFile /app/.htpasswd
require valid-user
</If>
# Is the domain local (i wanted to check two names)?
<If "%{SERVER_NAME} != 'localhost' && %{SERVER_NAME} != 'myproject.localhost'">
# I wanted to password protect the production server only
AuthType Basic
AuthName "Restricted area"
AuthUserFile /app/.htpasswd
require valid-user
</If>
I'm using Apache 2.4. Might not work for earlier versions.
我正在使用 Apache 2.4。可能不适用于早期版本。

