简体   繁体   English

我怎样才能拥有条件.htaccess块?

[英]How Can I Have A Conditional .htaccess Block?

This is an Apache question you've probably come across before. 这是您之前可能遇到的Apache问题。 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. 我希望有一个源代码包可以部署到我的工作站,我的登台服务器和我的生产服务器,但是它可以根据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. 注意我需要将SetEnv与这些重写捆绑在一起。 Currently if you use RewriteCond, it only ties to the following RewriteRule, but not the SetEnv underneath. 目前,如果你使用RewriteCond,它只会绑定到下面的RewriteRule,而不是下面的SetEnv。

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>

On Ubuntu Linux , the IfDefine 's variable is set in 在Ubuntu Linux上 ,设置了IfDefine的变量

/etc/apache2/envvars

and is called APACHE_ARGUMENTS . 并称为APACHE_ARGUMENTS So, at the bottom of that file I had to add: 所以,在该文件的底部我不得不添加:

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 . 但是,有一篇关于这个主题的Debian文章在这里讨论了这个问题 In that example, the file to edit is /etc/default/apache2 and the variable is called APACHE_DEFINES . 在该示例中,要编辑的文件是/ etc / default / apache2,该变量称为APACHE_DEFINES

Likewise, on some systems it is a variable named OPTIONS that 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. 因此,您真正需要做的是在apache2ctl文件中查找start部分。 So, begin by doing a whereis apache2ctl to 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. 因此,首先执行whereis apache2ctl以查找该脚本的位置,将其whereis apache2ctl并找到包含apache2指令的start部分,并查看它传递的变量是OPTIONSAPACHE_ARGUMENTSAPACHE_DEFINES还是其他内容。 Then, see which file you need to edit by experimentation with either /etc/sysconfig/httpd , /etc/default/apache2 , or /etc/apache2/envvars . 然后,通过使用/etc/sysconfig/httpd/etc/default/apache2/etc/apache2/envvars实验来查看需要编辑的文件。

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}/$1 [R=301,L]
</IfModule>

I tried the IfDefine method 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_ARGUMENTS line. 我尝试了IfDefine方法并且它不起作用,即使我传入Apache启动的已定义变量肯定出现在APACHE_ARGUMENTS行的phpinfo()

I tried another method and it worked perfectly. 我尝试了另一种方法,它工作得很好。 In your .htaccess file 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>

I'm using Apache 2.4. 我正在使用Apache 2.4。 Might not work for earlier versions. 可能不适用于早期版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM