2010-04-09 36 views
7

我在前端有一个apache,通过重写规则重定向请求。 我已经把基本的身份验证之前重定向请求,所以我把这个配置文件:apache:重写前的基本认证

<VirtualHost *:443> 
    ServerAdmin xxxxxx 
    DocumentRoot /var/www/html/ 
    ServerName xxxxxxx 
    RewriteEngine on 
    ErrorLog logs/error.log 
    CustomLog logs/access_log common 

    <Directory /var/www/html/> 
     AuthType Basic 
     AuthName "Restricted Files" 
     AuthUserFile /etc/httpd/conf/tag.pwd 
     Require valid-user 
     RewriteRule ^/(.*) http://xxxxxx:xxx/$1 [P,L] 
    </Directory> 
</VirtualHost> 

但不起作用。

有什么建议吗?

+0

你预计会发生什么?究竟发生了什么?你做了什么来实现它?你还尝试了什么? – 2010-04-10 14:11:35

+0

我认为所有请求后的身份验证将重定向与规则 RewriteRule^/(。*)http:// xxxxxx:xxx/$ 1 [P,L] 但这不会发生 Apache搜索页下/ var/www/html – pyro 2010-04-10 15:52:21

回答

5

我解决了把改写条件和重写规则Locatio指令外:

<Location /> 
    AuthType Basic 
    AuthName "Restricted Files" 
    AuthUserFile /etc/httpd/conf/tag.pwd 
    Require valid-user 
</Location> 
RewriteCond %{LA-U:REMOTE_USER} !^$ 
RewriteRule ^/(.*) http://xxxxxx:xxx/$1 [P,L] 

非常感谢h0tw1r3您的建议

*请记住,位置指令上的网址进行操作,而不是目录。这意味着如果有人为文档根目录创建了别名,他们将完全绕过这些认证规则。 (有关更多信息,请参见http://httpd.apache.org/docs/2.0/mod/core.html#location)。

2

更新:隐式目录规则确保在重写完成前始终需要验证。发现apache模块的不同组合改变了行为,因此接受的答案可能并不总是奏效。

<Location /> 
    AuthType Basic 
    AuthName "Restricted Files" 
    AuthUserFile /etc/httpd/conf/tag.pwd 
    Require valid-user 
</Location> 

<Directory /documentroot> 
    RewriteCond %{LA-U:REMOTE_USER} (.+) 
    RewriteRule (.*) http://xxxxxx:xxx/$1 [P,L] 
</Directory> 
+0

mmm它不起作用:(.apache继续在本地搜索页面 – pyro 2010-04-12 07:47:00

+0

最近这又出现了,即使pyro的接受的答案在Apache 2.2.22(Ubuntu)上失败了。 – h0tw1r3 2012-03-19 16:58:35

10

通常,Apache在授权阶段之前执行重写阶段,这就是为什么您的代码执行重写而不要求用户进行身份验证。

你可以用LA-U:REMOTE_USER变量来解决这个问题。与展望(“LA”)的授权阶段的条件前言你重写规则:

RewriteCond %{LA-U:REMOTE_USER} !^$ 
RewriteRule ^/(.*) http://xxxxxx:xxx/$1 [L] 

请参阅注意事项有关此内容http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond

正如其他海报指出,这也是更好地采取重写规则指令在块之外,所以他们更可靠。