2010-04-09 54 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

正如其他海報指出,這也是更好地採取重寫規則指令在塊之外,所以他們更可靠。