使用 URL 重写 (apache) 将 URL 作为参数传递


Passing an URL as a paramter using URL rewriting (apache)

我创建了一个.htaccess文件,我试图在其中重写某些URL。我的目标是将 URL 作为参数传递给另一个脚本。它有点工作,但我最终得到的 URL 在协议部分只包含一个正斜杠(例如 http:/而不是 http://)

这是我的.htaccess文件

DirectoryIndex index.php
php_flag register_globals off
php_flag magic_quotes_gpc off
php_value display_errors 0
FileETag none
ServerSignature Off
Options All -Indexes
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^test/(.*)$ test.php?url=$1 [QSA,NC,NE,L]
</IfModule>

现在,当我请求以下内容时:http://example.org/test/http://myurl.com,php $_REQUEST 对象包含一个包含 http:/myurl 的 url 变量.com

编辑当我请求:http://example.org/test/http%3A%2F%2Fmyurl.com 时,URL 根本没有被重写(以某种方式与重写正则表达式不匹配)

我似乎找不到这个问题的正确解决方案,转义字符似乎不是问题(因为那将是一个反斜杠)

实际上你不应该从RewriteRule中捕获URL,因为Apache用/替换了所有//

将您的规则替换为以下内容:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}'s/+test/([^'s]+) [NC]
    RewriteRule ^.*$ test.php?url=%1 [QSA,NE,L]
</IfModule>

THE_REQUEST' 变量表示 Apache 从您的浏览器收到的原始请求。

您的问题是在 URL 中斜杠被视为目录。解决你得到path/file path//file。这就是你的情况。

要正确编码它,您必须将http://myurl.com的斜杠替换为 %2Fhttp:%2F%2Fmyurl.com 。有关详细信息,请参阅percent encoding

更简单的解决方法是放弃一般http://