Apache RewriteRule将整个URL作为参数

时间:2015-11-03 18:03:05

标签: apache .htaccess mod-rewrite

目前,我的Apache RewriteRule仅将原始网址的路径作为查询参数传递给重写的网址。

如何将整个网址(包括方案和权限等)作为参数发送到重写的网址?

我知道%{REQUEST_URI}只传递路径,我看不到任何传递整个网址的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]

目前我已将url硬编码到查询参数中,因此URL

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

变为:

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

1 个答案:

答案 0 :(得分:3)

您可以使用此规则将完整网址作为查询参数:

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]