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

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

问题描述

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

我希望根据开发环境和实时环境将条件逻辑添加到我的 .htaccess 文件中.例如,我想在实时服务器上进行身份验证,而不是在开发服务器上进行身份验证.我在我的 httpd.conf

SetEnv DEV 1

我已通过检查 phpinfo() 的输出确认此变量已设置.然后在我的 .htaccess 文件

AuthType 基本AuthName "密码保护"AuthUserFile/path/to/.htpasswd需要有效用户</IfDefine>

...但我的本地开发人员仍然提示我输入密码..htaccess 似乎无法使用 DEV 变量.我确实在我的 httpd.conf 中为我的文档根设置了 AllowOverride All.有什么想法吗?

解决方案

我对这个问题刚刚解决了大约 4 个小时,我相信我有了最终的答案,并且可以为大家总结如何解决这个特别痛苦的问题.

我使用 Windows 7 Home Premium 和 Apache 2.2x 和 PHP 5.3 作为我的开发机器.我也想要一个 DEV 环境变量,我可以在我的 .htaccess 文件中使用它来关闭重写和其他在我的开发环境中无效但对我的生产环境至关重要的指令.

我的 .htaccess 文件是这样的;

添加类型应用程序/x-httpd-php53 .php</IfDefine>

HostGator 告诉我,为了拥有 php 5.3,我需要像这样修改我的 htaccess 文件以启用它,否则我只有 php 5.2.但是我已经在我的开发机器上安装了它,所以当我在本地查看它时,这个指令导致我的客户网站崩溃.我将要解释的所有内容都允许我在我的 Git 存储库中保留一个 .htaccess 文件,该文件在两个位置都可以使用.

首先,让我把我在网上搜索使用IfDefine和SetEnv的方法来解决这个问题的过程中所学到的所有东西进行清楚/总结;

  1. Apache 中的 IfDefine 指令, Only , ONLY ,当我说 only 我的意思是 ONLY 时,响应在命令行传递的参数.让我稍微强调一下.只有命令行!
  2. SetEnv 和 SetEnvIf,是两个完全不同的东西.一个 (SetEnv) 用于 conf 文件,设置在 SERVER START TIME 设置的环境变量(特定于 apache).SetEnfIf 在 REQUEST TIME 使用,仅用于根据 REQUEST 变量确定要设置的内容.
  3. IfDefine 指令不读取由 SetEnv 或 SetEnvIf 设置的变量.时期.没有争论,没有问题,没有但我认为……"不.它没有,所以克服它.

简短的回答是否定的,您不能只在 httpd.conf 中使用SetEnv DEV 1",然后使用 IfDefine 在您的 .htaccess 文件中检测它,根据语法和性质,这看起来很直观和合理我们每个人都习惯的编程逻辑.回想一下,我们实际上并没有编程任何东西,这些是配置文件,当然它们不符合这种期望仅仅是因为它们看起来应该如此.

答案

所以这意味着我必须弄清楚如何向 Apache 添加启动参数,对于 Linux 人员来说,这个答案很容易获得,您只需要向 envvars 文件,但是我们这些可怜的 windows 迷呢?

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

  1. Windows 不允许您在 Apache2.2 的服务配置中永久添加启动参数(它不起作用,不要尝试,我已经做过一百万次了,相信我).确实如此,如果您进去尝试输入自己的参数,它只会工作一次,然后下次打开对话框时参数字段为空.我不知道为什么会这样,但这些参数似乎是为了测试,而不是永久修改.
  2. 安装 Apache 后,它会在开始菜单中创建开始"、停止"和重新启动"快捷方式,并安装 Apache 服务监视器.但是 开始菜单中的快捷方式使用的启动参数与 apache 服务监视器使用的启动参数不同.因此,如果您使用这些方法的组合启动/停止 apache,您将获得不同的结果,具体取决于您使用的方法.但是,您可以将 -D "__DEV__" 放在开始菜单快捷方式中,它会起作用!

解决方法

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

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

  2. 修改它以包含新的 -D __DEV__ 变量,该变量必须紧跟在 httpd.exe 之后;"C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin\httpd.exe" -D "__DEV__" -w -n "Apache2.2" -k start

  3. 您的开始菜单快捷方式现在将使用您的 dev 变量启动 apache.

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

  5. 输入:net stop apache2.2(或任何你为 apache 使用的服务名称)

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

  7. 注意将start 一词更改为config.这个神奇命令的作用是将您在屏幕上看到的设置保存到 Windows 中与服务一起存储的设置中.按回车键.从现在开始,每当您启动服务、Apache 服务监视器启动服务或 Windows 启动服务时,都会传递您的变量.

对不起大家的小说,我希望它可以帮助其他一些疲惫的灵魂总结和解释所有这些信息,我知道它会帮助我!:D

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

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

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>

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

解决方案

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.

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.

My .htaccess file looks like this;

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

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.

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. The IfDefine directive in Apache, Only , ONLY and when I say only i mean ONLY, responds to parameters passed at the command line. Let me emphasize that a little. ONLY COMMAND LINE!
  2. SetEnv and SetEnvIf, are two entirely different things. One (SetEnv) is for use in the conf files, setting environment variables (specific to apache) which are set at SERVER START TIME. SetEnfIf is used at REQUEST TIME and is only used to determine what to set based on REQUEST variables.
  3. The IfDefine directive does not read variables set by SetEnv or SetEnvIf. Period. There's no argument, there's no question, there's no "but i thought..." NO. It doesn't, so get over it.

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.

The Answer

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?

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

  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!

Steps to Solve It

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

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

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

  4. Go to a command line (as administrator)

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

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

  7. Note the change of the word start to config. What this magical command does is saves the settings you are seeing on the screen to the settings stored with the service in Windows. Hit Enter. From this point forward your variable will be passed whenever you start the service, the Apache Services Monitor starts the service, or windows starts the service.

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天全站免登陆