【问题标题】:Apache RewriteRule pass entire URL as a parameterApache RewriteRule 将整个 URL 作为参数传递
【发布时间】:2015-11-04 02:51:30
【问题描述】:

目前我的 Apache RewriteRule 仅将原始 URL 的路径作为查询参数传递给重写后的 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]

目前我已将 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

【问题讨论】:

  • apache是​​哪个版本的?

标签: apache .htaccess mod-rewrite


【解决方案1】:

您可以使用此规则获取完整 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]

【讨论】:

  • 谢谢,%1 到底是在哪里定义的?每个规则是否返回相应的索引值?比如%0%1等等?
  • %1 是我们从这里的第二个RewriteCond 得到的。对于https,它可以是s,对于http,它可以是一个空字符串。
  • 如果是 Apache 2.4,使用 "%{REQUEST_SCHEME}://...` 也可以 =)
  • 谢谢,没错,我也会更新 Apache 2.4 的答案。
猜你喜欢
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 2012-04-03
  • 2010-12-18
相关资源
最近更新 更多