2015-11-03 268 views
3

目前,我的Apache RewriteRule僅將原始URL的路徑作爲查詢參數傳遞到重寫的URL。Apache RewriteRule將整個URL作爲參數傳遞

如何將整個URL(包括方案和權限等)作爲參數發送到重寫的URL?

我知道%{REQUEST_URI}只通過路徑,我看不到任何可以傳遞整個URL的Apache環境變量。

我是一個初學者的Apache配置的東西,所以請原諒我的一部分的任何無知,

的感謝!

這裏是我當前的配置:

# 
# Enable Rewrite 
# 

RewriteEngine On 

# 
# Condition for rewriting the URL. Check the URL's path is a valid REST URI path 
# 

RewriteCond %{REQUEST_URI} ^(?:[^?#]*)(?:/v[0-9]+(?:\.[0-9]+)*)(?:/[a-z]+)(?:/[a-z]+)(?:/?[^/?#]*)(?:[^?#]*)$ 

# 
# Rewrite the URL, sending the REST path as a parameter to the specified version. 
# 

RewriteRule ^(?:[^?#]*)(?:/(v[0-9]+(?:\.[0-9]+)*))((?:/[a-z]+)(?:/[a-z]+)(?:/?[^/?#]*)(?:[^?#]*))$ https://localhost/rest/$1/index.php?request_uri_path=https://localhost/rest/$1/$2 [QSA,NC,L,R=307] 

目前,我已經硬編碼的網址查詢參數,以便網址

https://localhost/rest/v1/users/show/12345 

變爲:

https://localhost/rest/v1/index.php?request_uri_path=https://localhost/rest/v1/users/show/12345 
+0

哪個版本的Apache的是什麼呢? – hjpotter92

回答

3

您可以使用此規則獲取完整的URL作爲查詢參數:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{HTTPS}s on(s)| 
RewriteRule^index.php?request_uri_path=http%1://%{HTTP_HOST}%{REQUEST_URI} [L,QSA] 

如果你在Apache 2.4,那麼你可以使用%{REQUEST_SCHEME}變量:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule^index.php?request_uri_path=%{REQUEST_SCHEME}://%{HTTP_HOST}%{REQUEST_URI} [L,QSA] 
+0

謝謝,確切定義'%1'的位置在哪裏?每個規則是否返回相應的索引值?例如'%0','%1'等等? –

+0

'%1'是我們從第二個'RewriteCond'這裏得到的。它可以是''''''''''或'http'的空字符串。 – anubhava

+1

有趣的,謝謝:) –