Php htacess重写规则在查询字符串中返回错误值

Php htacess重写规则在查询字符串中返回错误值,php,regex,.htaccess,url-rewriting,Php,Regex,.htaccess,Url Rewriting,我有一个重写规则,但它没有按预期工作 重写规则 RewriteRule ^([^/]+)$ category.php?cat_alias=$1 在服务器端,我将文件名作为请求中的值 Array ( [cat_alias] => category.php ) 下面是此重写规则的示例url 预期结果: Array ( [cat_alias] => news ) 问题是您的规则没有条件,它正在将任何不是/的内容写入category.php。重

我有一个重写规则,但它没有按预期工作

重写规则

RewriteRule ^([^/]+)$ category.php?cat_alias=$1
在服务器端,我将文件名作为请求中的值

Array
(
    [cat_alias] => category.php
)
下面是此重写规则的示例url

预期结果:

Array
    (
        [cat_alias] => news
    )

问题是您的规则没有条件,它正在将任何不是
/
的内容写入
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]