2013-03-07 30 views
0

我試圖重寫不存在的URL來使用我的index.php文件,同時還將HTTP:Authorization環境變量附加到我的腳本中。到目前爲止,我一次只能得到一個或另一個工作。不是都。有人能告訴我我的.htaccess中的錯誤在哪裏?Apache .htaccess配置問題中的多個RewriteConditions

RewriteEngine on 

# Get HTTP authorization 
RewriteCond %{HTTP:Authorization} ^Basic.* 

# Append to URL 
RewriteRule (.*) index.php?_auth=%{HTTP:Authorization} [QSA,L] 

# If a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Otherwise forward it to index.php 
RewriteRule (.*) index.php?_auth=%{HTTP:Authorization} [QSA,L] 
+0

我不知道你爲什麼這樣做,當然授權頭必須在PHP中可訪問? – kjetilh 2013-03-07 22:31:34

回答

1

首先,您的.htaccess在我的測試環境中工作正常。我收到了每個請求的_auth參數。

但你並不需要所有這些RewriteCond S,只是一個RewriteRule

RewriteRule .* index.php?_auth=%{HTTP:Authorization} [QSA,L] 

這將改寫所有請求index.php,將授權頭作爲_auth參數。

如果您只希望用_auth參數重寫不存在的URL,請將RewriteCond s預設爲RewriteRule。如果緩存是你的情況的問題,添加RewriteCond HTTP:Authorization以及

RewriteEngine on 

RewriteCond %{HTTP:Authorization} ^Basic 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* index.php?_auth=%{HTTP:Authorization} [QSA,L] 
+0

很明顯,她需要的條件,否則像圖像和腳本的所有資源被重寫爲index.php – kjetilh 2013-03-07 22:22:08

+0

@kjetilh它們將被重寫與原始.htaccess以及,除非其他recources單獨使用單獨的.htaccess。 – 2013-03-07 22:25:11

+0

好的,在我看來,她需要將在授權標題中查找*^Basic *的第一個條件移至底部規則,並取消第一條規則 – kjetilh 2013-03-07 22:35:00

0

我敢肯定Authorization頭是通過PHP訪問,所以它似乎沒有必要追加值作爲查詢參數。

查看$_SERVER超全球,或許是關鍵AUTH_TYPE是你在找什麼?

<?php $_SERVER['AUTH_TYPE']) ?> 

無論如何,如果你依然堅持對的.htaccess解決方案,然後你真的可以只廢棄整個第一條規則一起;如果存在,_auth參數將附加授權標頭。

# If a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Otherwise forward it to index.php 
RewriteRule (.*) index.php?_auth=%{HTTP:Authorization} [QSA,L]