【问题标题】:htacess rewrite rule return wrong value in query stringhtaccess rewriterule 在查询字符串中返回错误值
【发布时间】:2020-11-06 20:25:07
【问题描述】:
我有一个重写规则,但它没有按预期工作。
重写规则。
RewriteRule ^([^/]+)$ category.php?cat_alias=$1
在服务器端,我将文件名作为请求中的值
Array
(
[cat_alias] => category.php
)
以下是此重写规则的示例 url
http://www.example.com/news
预期结果:
Array
(
[cat_alias] => news
)
【问题讨论】:
标签:
php
regex
.htaccess
url-rewriting
【解决方案1】:
问题是您的规则没有条件,并且它正在将任何不是/ 的内容写入category.php。在它重写/news 之后,规则在下一个循环中再次执行,并再次重写/category.php。
需要添加重写条件,防止已有文件和目录被重写,防止循环。
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ category.php?cat_alias=$1 [L,QSA]