首页 > 解决方案 > 重写规则后无法在php中获取参数

问题描述

重写规则后尝试从 url 获取参数时出现问题

这是我的网址:

www.mysite.com/pages/example/two/

这个应该加载我的重写规则

www.mysite.com/pages/page.php?user=two

以下是我的重写规则

RewriteEngine on
RewriteRule ^example/(.+)/?$ page.php?user=$1 [NC,L]

我有两个参数的第二条规则

RewriteRule ^example/(.+)/(.+)$ page.php?user=$1&lang=$2 [NC,L,QSA]

我的 php 中有一个var_dump ($_GET['user']);,但我得到的结果为NULL和 PHP 注意:未定义的索引:第 4 行 /path/page.php 中的用户。

任何想法?提前致谢。

标签: php.htaccess

解决方案


尝试这个:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^example/(.*)/?$ page.php?user=$1 [NC,QSA]
RewriteRule ^example/(.*)/(.*)$ page.php?user=$1&lang=$2 [NC,QSA]

推荐阅读