阿帕奇IfDefine条件句在.htaccess阿帕奇、条件、IfDefine、htaccess

2023-09-02 00:29:31 作者:你是我年少时最蔚蓝的海

我的开发设置:Mac OSX版10.7.4 /阿帕奇2.2.21 / 5.3.10 PHP

my dev setup: Mac OSX 10.7.4 / Apache 2.2.21 / PHP 5.3.10

我希望有条件的逻辑添加到我的.htaccess文件取决于开发VS现场环境。例如我希望有活的服务器上,但不是dev的服务器上进行验证。我在我的的httpd.conf

i wish to add conditional logic to my .htaccess files depending on dev vs live environment. for example i want to have authentication on the live server but not on the dev server. i have in my httpd.conf

SETENV DEV 1

我已经确认这是变种通过检查的phpinfo()输出设置。然后在我的的.htaccess 文件

i have confirmed that this var is set by checking the output from phpinfo(). then in my .htaccess file

<IfDefine !DEV>
AuthType Basic
AuthName "password protected"
AuthUserFile /path/to/.htpasswd
Require valid-user
</IfDefine>

但我还是提示输入密码,在我的本地开发。看来在 DEV 变量不可为的.htaccess 。我有的AllowOverride全部在我的设置的httpd.conf 我的文档根。有任何想法吗?谢谢。

but i am still prompted for password on my local dev. it appears that the DEV variable is not available to .htaccess. i do have AllowOverride All set in my httpd.conf for my doc root. any ideas? thanks.

推荐答案

我是断食约4小时到这个问题,我相信我有最后的答案,并能为大家总结一下如何解决这个特别不好受问题。

I am fresh off of about 4 hours into this problem, and I believe I have the final answer and can summarize for everyone how to solve this particularly painfull problem.

我使用Windows 7家庭premium与Apache 2.2倍和PHP 5.3作为我的开发机。 我也希望有一个 DEV 环境变量,我可以在我的.htaccess文件使用关闭重写等指令不属于我的农业开发环境,有效的,但对我的生产环境是至关重要的。

I am using Windows 7 Home Premium with Apache 2.2x and Php 5.3 as my dev machine. I too want to have a DEV environment variable, which I can use in my .htaccess files to turn off Rewriting and other directives which are not valid on my develpment environment but are critical to my production environment.

我的.htaccess文件看起来像这样;

My .htaccess file looks like this;

<IfDefine !__DEV__>
    AddType application/x-httpd-php53 .php
</IfDefine>

HostGator的告诉我,为了让PHP 5.3,我需要修改我的htaccess的文件中像这样来启用它,或者我只让PHP 5.2。但我已经有它在我的dev的机器的话,这个指令是导致当我看到它在本地我的客户的网站崩溃。一切,我要解释的过程让我保留一个.htaccess文件在我的Git仓库,在这两个地区,其中的工作原理。

HostGator informed me that in order to have php 5.3 I needed to modify my htaccess file like this to enable it or I'd only have php 5.2. But I already have it on my dev machine so, this directive was causing my customer website to crash when I viewed it locally. Everything I'm about to explain has allowed me to keep ONE .htaccess file in my Git Repository, which works in both locations.

首先,让我清晰/总结所有的东西,我学过的冲刷互联网的使用IfDefine和SETENV解决这一问题的途径;

First, let me clear/sum up all the things I learned while scouring the internet for the way to use IfDefine and SetEnv to solve this issue;

在阿帕奇的IfDefine指令,只有,只有当我说的只有我的意思是ONLY,响应在命令行中传递的参数。我要强调的一点。唯一的命令行了! SETENV和SetEnvIf之后,是两个完全不同的事情。一(SETENV)可以在conf文件中使用,设置环境变量(针对Apache),它被设置在服务器启动时。 SetEnfIf用于在请求时,并仅用于确定要一套基于请求变量。 的IfDefine指令不能读取SETENV或SetEnvIf之后设置的变量。期。有没有说法,毫无疑问,有没有,但我以为......NO。它没有,所以得到了它。

简短的答案是否定的,你不能只用SETENV DEV 1在httpd.conf,然后用IfDefine检测到它在你的.htaccess文件,这似乎直观,合理的基础上的语法和性质编程逻辑我们任何人都习惯了。回想一下,我们是不是其实编程什么,这些是配置文件,当然,他们不符合这种期望仅仅是因为它看起来像他们应该。

The short answer is NO, you can't just use "SetEnv DEV 1" in httpd.conf and then use IfDefine to detect it in your .htaccess file, which would seem intuitive and reasonable based on the syntax and nature of programming logic any of us are used to. Recall that we are not in fact programming anything, that these are config files and of course they don't conform to this expectation simply because it seems like they should.

答案

因此​​,这意味着我必须弄清楚如何启动参数添加到Apache,以及为Linux专家,答案是现成的,你只需要正确的东西添加到 envvars中文件,但对于我们这些可怜的窗口瘾君子?

So this means that I have to figure out how to add a startup parameter to Apache, well for the Linux Guys, that answer is readily available, you just have to add the right stuff to the envvars file, but what about us poor windows junkies?

那么对于Windows用户来说变得更有趣,原因如下:

Well for windows users it gets more fun for the following reasons:

在Windows不允许你到永久的的Apache2.2服务配置中添加启动参数(这是行不通的,不要尝试它,我已经做到了一百万次了,相信我)。这是真实的,如果你去那里,并尝试把自己的参数,它只能工作一次,然后在参数字段为空,下一次你打开对话框。我不知道为什么这样的话,但似乎这些参数的目的是用于测试,而不是永久性变形例。 当Apache安装它创建开始,停止和重新启动,在开始菜单的快捷方式,以及安装的Apache服务监视器。 但的开始菜单快捷方式使用比所使用的Apache的服务监控不同的启动参数。所以,如果你开始/停止Apache使用这些方法的组合,你会得到的不同的结果取决于你使用什么方法。但是,你可以把-D__DEV__在开始菜单的快捷方式,它会工作! Windows does not allow you to permanently add startup parameters in the services configuration for Apache2.2 (it doesn't work, don't try it, I've done it a million times, trust me). This is true, if you go in there and try to put in your own parameters, it will only work one time and then the parameter field is empty the next time you open the dialog. I don't know why this is the case, but it seems that those parameters are intended for testing, not a permanent modification. When Apache is installed it creates "Start", "Stop" and "Restart" shortcuts in the start menu, as well as installs the Apache Services Monitor. BUT the shortcuts in the start menu use different startup parameters than those used by apache services monitor. So if you start/stop apache using a combination of these methods you will get different results depending on what method you used. However, you can put the -D "__DEV__" in the start menu shortcut and it will work!

的措施来解决它

要永久和普遍设置一个__DEV__环境变量,你可以参考在.htaccess文件中使用IfDefine,在Windows开发环境将工作无论你使用的是服务,或在开始菜单中的快捷方式或使用NET START启动Apache / STOP在命令行中,执行以下操作:

To permanently and universally setup a __DEV__ environment variable which you can reference using IfDefine in .htaccess files, on a Windows Development environment which will work whether you start Apache using a service or the shortcuts in the start menu or using NET START/STOP on the command line, do the following:

打开的属性开始菜单快捷方式,并提取您找到启动Apache那里的命令。我的是; C: Program Files文件(x86)的 Apache软件基金会的Apache2.2 斌 httpd.exe-w -n的Apache2.2-k启动

Open the properties for the start menu shortcut and extract the command you find for starting Apache there. Mine was; "C:Program Files (x86)Apache Software FoundationApache2.2binhttpd.exe" -w -n "Apache2.2" -k start

修改它以包括新的-D __DEV__变量,它必须走在启动后立即httpd.exe; C: Program Files文件(x86)的 Apache软件基金会的Apache2.2 斌 httpd.exe -D__DEV __ -w -n的Apache2.2-k启动

Modify it to include the new -D __DEV__ variable, which MUST go at the start immediately following httpd.exe; "C:Program Files (x86)Apache Software FoundationApache2.2binhttpd.exe" -D "__DEV__" -w -n "Apache2.2" -k start

您开始菜单快捷方式将现在启动Apache与地方你的开发变量。

Your start menu shortcut will now start apache with your dev variable in place.

进入命令行(管理员)

类型:净停止的Apache2.2(或任何你的服务名称是Apache)

Type: net stop apache2.2 (or whatever your service name is for apache)

现在输入(或复制粘贴)相同的命令,用在开始菜单的快捷方式上面进入命令行,但做如下改变它; C: Program Files文件(x86)的 Apache软件基金会的Apache2.2 斌 httpd.exe-D__DEV__-w -n的Apache2.2-k 配置

Now type in (or copy-paste) the same command as is used in the start menu shortcut above into the command line but make the following change to it; "C:Program Files (x86)Apache Software FoundationApache2.2binhttpd.exe" -D "__DEV__" -w -n "Apache2.2" -k config

抱歉小说大家,我希望它能帮助其他一些疲惫的心灵在那里有这一切信息汇总和解释,我知道这会帮助我! :D

Sorry for the novel everyone, I hope it helps some other weary soul out there to have all this info summarized and explained, I know it would have helped me! :D