如何使用htaccess从url字符串访问变量?

时间:2013-03-06 12:33:13

标签: php .htaccess

我有像这样的htaccess

RewriteRule i_am_(.*)_fan([0-9]).html$ /fans.php?player=$1&page=$2

当我输入此http://messi_fans.com/i_am_messi_fan2.html的网址时,它会重定向到页面http://messi_fans.com/fans.php。但 GET 变量是

Array ( [player] => messi_fan2 [page] => ) 

我希望 GET 像这样Array ( [player] => messi [page] =>2 ) 如何修改上面的htaccess?

3 个答案:

答案 0 :(得分:1)

首先至少使用基本指令和一些条件来阻止循环,如下所示:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !fans\.php                        [NC]
RewriteCond %{REQUEST_URI}  ^/i_am_([^_]+)_fan([^.]+)\.html/? [NC]
RewriteRule .*              fans.php?player=%1&page=%2        [L]

静默地映射

带有或不带斜线的

http://messi_fans.com/i_am_messi_fan2.html

要:

http://messi_fans.com/fans.php?player=messi&page=2

对于永久和可见重定向,将[L]替换为[R = 301,L]

答案 1 :(得分:0)

您应该在.*附加问号时使用lazy quantifier

i_am_(.*?)_fan([0-9]).html$

答案 2 :(得分:0)

应该是

RewriteRule i_am_([a-zA-Z])_fan([0-9]).html$ /fans.php?player=$1&page=$2