Apache RewriteRule pass entire URL as a parameter

僤鯓⒐⒋嵵緔 提交于 2019-12-23 13:15:39

问题


Currently my Apache RewriteRule only passes the path of the original URL as a query parameter to the rewritten URL.

How do I send the entire URL (including scheme and authority etc) as a parameter to the rewritten URL?

I know %{REQUEST_URI} only passes the path, and I can’t see any Apache environment variables that do pass the entire URL.

I’m something of a beginner to Apache configuration so excuse any ignorance on my part,

thanks!

Here’s my current config:

#
# 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]

Currently I’ve hardcoded the url into the query parameters so the URL

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

becomes:

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

回答1:


You can use this rule to get full URL as query parameter:

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

If you're on Apache 2.4 then you can make use of %{REQUEST_SCHEME} variable:

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


来源:https://stackoverflow.com/questions/33506089/apache-rewriterule-pass-entire-url-as-a-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!