mod_rewrite和完整的url,参数为参数

时间:2014-03-03 19:36:37

标签: php regex apache .htaccess mod-rewrite

我希望获得带有参数的完整url到index.php,用mod_rewrite重写

的.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php?q=$1 [L,QSA]

index.php只是

print_r($_GET);

当我尝试

domain.tld/http://asdf/asdf&s=1%3fb=3&c=34

我得到了

Array
(
    [q] => http://asdf/asdf
    [s] => 1?b=3
    [c] => 34
)

但我需要这样的东西:

Array
(
    [q] => http://asdf/asdf&s=1%3fb=3&c=34 //some complicated url
)

有没有(简单)方式?我找到了许多不同参数的解决方案,而不是一体化的解决方案。 对不起我的英文:)

2 个答案:

答案 0 :(得分:3)

您可以在根目录中使用此规则.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(\S+)
RewriteRule ^ /index.php?q=%1 [B,L]

答案 1 :(得分:1)

问题在于,在您的示例URI中,查询字符串格式不正确,导致Apache将字符串(that part starting with& s = ...`的一部分解释为查询字符串。 / p>

实质上,这意味着Apache认为这是输入:

URI => http://asdf/asdf
Query String => s=1%3fb=3&c=34

重定向规则仅对URI部分进行操作,然后通过QSA标志附加查询字符串,因此您将获得类似以下内容的重写请求:

index.php?q=http://asdf/asdf&s=1%3fb=3&c=34

您需要先对查询字符串进行URL编码。例如,可能与以下内容类似:

$query_string = urlencode('http://asdf/asdf&s=1?b=3&c=34');

然后为防止Apache重新编码(双重编码),在重定向规则中使用NE标志:

RewriteRule (.*)$ index.php?q=$1 [L,NE,QSA]