Mod_rewrite和PHP问题

时间:2009-07-27 09:55:10

标签: apache mod-rewrite

我正在尝试编写这个重写代码,它只是不起作用(apache 2.2):

RewriteEngine on
RewriteBase /

RewriteRule ^account/activate\?token=(.*) index.php?route=account/activate&token=$1

我尝试了很多不同的变化。在尝试了大约和小时并搜索谷歌后我感到难过。这是你进来的地方!

4 个答案:

答案 0 :(得分:3)

试试这个

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^account/activate$ index.php?route=account/activate&%1

基本上,这会将整个查询字符串映射到RewriteRule中的%1,然后您可以使用旧的常规$ _GET ['token']从您的php脚本访问您的令牌

答案 1 :(得分:2)

RewriteRule directive的模式仅在URI路径上再次测试。查询字符串只能使用RewriteCond directive

进行测试
RewriteCond %{QUERY_STRING} ^token=(.*)
RewriteRule ^account/activate$ index.php?route=account/activate&token=%1

但是有一个更简单的解决方案:只需设置QSA flag即可将查询字符串自动附加到新的查询字符串中:

RewriteRule ^account/activate$ index.php?route=account/activate [QSA]

注意:如果在替换中定义了查询,则只需设置QSA标志。如果没有,原始查询已经自动附加。

答案 2 :(得分:0)

我用过这个:

RewriteEngine On

RewriteRule ^account/activate/?$ index.php?route=account/activate&%{QUERY_STRING}
RewriteRule ^account/activate/?/$ index.php?route=account/activate&%{QUERY_STRING}

它适用于此:

<?php 

echo "route is: " . $_GET['route'];
echo "</br>";
echo "token is " . $_GET['token'];

我的代码所在的目录名为test。 这些是我的网址:

http://localhost:80/test/account/activate/?token=test
http://localhost:80/test/account/activate/?token=asd

答案 3 :(得分:0)

您无法将网址的参数部分与mod_rewrite匹配。

在网址中测试参数的唯一方法是使用RewriteCond指令并测试env变量QUERY_STRING