Apache .htaccess-根据环境或主机名有条件地应用基本身份验证 [英] Apache .htaccess - applying basic authentication conditionally based on environment or hostname

查看:89
本文介绍了Apache .htaccess-根据环境或主机名有条件地应用基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的开发人员设置:Mac OSX 10.7.4 / Apache 2.2.21 / PHP 5.3.10

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

我希望根据以下情况向我的.htaccess文件添加条件逻辑:开发人员与现场环境。例如,我想在实时服务器上进行身份验证,而不在开发服务器上进行身份验证。我在我的 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 不可用。我的文档根目录确实在 httpd.conf 中设置了 AllowOverride All 。有任何想法吗?

...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?

推荐答案

我刚经历了大约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 Home Premium和Apache 2.2x和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。但是我已经在开发机器上安装了它,因此,当我在本地查看该指令时,它导致我的客户网站崩溃。我要解释的所有内容都使我可以在我的Git存储库中保留一个.htaccess文件,该文件在两个位置都可以使用。

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;


  1. Apache中的IfDefine指令,只有,只有当我说我的意思是,才响应在命令行中传递的参数。让我强调一下。

  2. SetEnv和SetEnvIf是两个完全不同的东西。一个(SetEnv)用于conf文件,设置在SERVER START TIME设置的环境变量(特定于apache)。 SetEnfIf在请求时间使用,仅用于根据请求变量确定要设置的内容。

  3. IfDefine指令不读取由SetEnv或SetEnvIf设置的变量。期。没有论点,没有问题,没有但我想...不。

简短的回答是否,您不能只使用 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 文件中添加正确的内容,但是对我们这些可怜的 windows 垃圾邮件呢?

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:


  1. Windows不允许您在Windows操作系统中永久添加启动参数。 Apache2.2的服务配置(不起作用,请不要尝试,我已经做了一百万遍了,相信我)。的确如此,如果您进入那里并尝试输入自己的参数,它将只能运行一次,然后在下次打开对话框时,参数字段为空。我不知道为什么会这样,但是这些参数似乎是用于测试,而不是永久修改。

  2. 安装Apache后,它会创建开始,停止 开始菜单中的和重新启动快捷方式,以及安装Apache Services Monitor。 开始菜单中的快捷方式使用的启动参数与apache服务监视器使用的启动参数不同。因此,如果您结合使用这些方法来启动/停止a​​pache,您将获得不同的结果,具体取决于您使用的是哪种方法。但是,您可以在开始菜单快捷方式中放入-D __DEV__,它将起作用!

  1. 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.
  2. 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!

解决步骤

要在Windows开发环境中永久且通用地设置__DEV__环境变量,您可以在Windows开发环境中使用IfDefine在htaccess文件中引用该变量。使用服务或开始菜单中的快捷方式或在命令行上使用NET START / STOP启动Apache,请执行以下操作:

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:


  1. 打开开始菜单快捷方式的属性,然后提取您找到的用于在那里启动Apache的命令。我的是 C:\Program Files(x86)\Apache Software Foundation\Apache2.2\bin\httpd.exe -w -n Apache2.2 -k start

  1. 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 Foundation\Apache2.2\bin\httpd.exe" -w -n "Apache2.2" -k start

修改它以包括新的-D __DEV__变量,该变量必须在httpd.exe之后立即开始; C:\Program Files(x86)\Apache Software Foundation\Apache2.2\bin\httpd.exe -D __DEV __ -w -n Apache2.2 -k start

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 Foundation\Apache2.2\bin\httpd.exe" -D "__DEV__" -w -n "Apache2.2" -k start

您的开始菜单快捷方式现在将在您的dev变量就位的情况下启动apache。

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

转到命令行(以管理员身份)

Go to a command line (as administrator)

类型:net stop apache2.2(或您为Apache使用的任何服务名称)

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

现在在命令行中键入(或复制粘贴)与上面开始菜单快捷方式中使用的命令相同的命令,但要对其进行以下更改; C:\程序文件(x86)\ Apache Software Foundation\Apache2.2\bin\httpd.exe -D __DEV__ -w -n Apache2.2 -k config

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 Foundation\Apache2.2\bin\httpd.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

这篇关于Apache .htaccess-根据环境或主机名有条件地应用基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆