如何在PHP中从.htaccess重写的URL中获取GET查询字符串参数

3

在我的网站上,我正在尝试将一些URL重写为如下形式:

这个URL

http://example.com/news.php?newstype=newsletter&newsid=1123&newstitle=my news title

变为:

http://example.com/newsletter/1123/my news title

我的.htaccess中的重写规则如下所示:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /

RewriteRule ^(.*)/(.*)/(.*)/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]

我的 <a> 标签看起来像这样:

$seoUrl = "$urlNewsType/$newsid/$urlNewsTitle/";
$seoUrl = urldecode($seoUrl);
$seoUrl = html_escape($seoUrl, 'UTF-8');

<a href="$seoUrl">Read More</a>

这些东西对我有作用。但我的问题是,我需要将这些查询字符串参数从URL分别获取到PHP中。问题是我不能像通常那样获得这些参数。
在php中,以下是输出: echo '<pre>',print_r($_GET).'</pre>';
Array
(
  [newstype] => news/1114
  [newsid] => Presentation: Heath+Day+in+2018
  [newstitle] => 
)

有人能告诉我,在这方面我做错了什么吗?

1个回答

3
正则表达式模式(.*)是贪心的。它匹配完整的请求路径。请尝试使用([^/]+)代替:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ news.php?newstype=$1&newsid=$2&newstitle=$3 [L,QSA,NC,B]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接