我需要一个解决方案来解决我的重定向问题。在 .htaccess
文件中,我有以下代码:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+) category.php?page=$1 [NC,L,QSA]
这适用于像这样的查询
category.php?page=pinturas
并重定向到 category/pinturas
。
但是,如果字符串有这样的 -
:
category.php?page=aquoso-madeira
它重定向到 category/aquoso
有什么帮助吗?
最佳答案
category.php?page=aquoso-madeira
It redirect tocategory/aquoso
因为您的 RewriteRule
模式 不包含连字符 (-
),因此它匹配第一个连字符之前的所有内容,但不包括第一个连字符.
在字符类中包含连字符:
RewriteRule ^category/([a-zA-Z0-9-]+) category.php?page=$1 [NC,L,QSA]
请注意,连字符 (-
) 必须位于字符类的开头或结尾,因为这是一个特殊字符,在其他地方使用时具有其他含义。
And this working for query like category.php?page=pinturas and redirect to category/pinturas
请注意,此重写与您所描述的完全相反。
此规则接受类似 category/pinturas
的请求,并在内部将其重写为 category.php?page=pinturas
。 (这里没有外部重定向 - 它可能位于代码的另一部分?)
关于apache - htaccess URL 重写 page.php?page=包含连字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46505346/