2015-11-03 267 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

有趣的,谢谢:) –