使用.htaccess重写URL只适用于第一个查询字符串


rewrite URL using .htaccess only works on first query string

我试图使用htaccess重写我的url,但它只对第一个查询字符串有效

我想要的是重写这个URL

acnologia.com/index.php?page=images&tag=nature
into something like this acnologia.com/images/nature

它确实适用于第一个查询字符串acnologia.com/images但如果我在"图像"后添加另一个目录,则不起作用

这是我的.htaccess代码:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1&tag=$2

原始规则中只有一个组,因此没有$2。

试试这个:

RewriteRule ^([a-z'd_-]+)/([a-z'd_-]+)$ index.php?page=$1&tag=$2 [NC,L] 

''d等于0-9,NC表示无碱基

最基本的规则如下

RewriteRule ^(.*)/(.*)$ index.php?page=$1&tag=$2

更具体的是

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2

对此@Andrew在评论中做出了回应。

您的最终重写规则应该如下。指定了RewriteCond,使得js、css、png等文件与此重写规则不匹配。

RewriteEngine On
RewriteCond %{REQUEST_URI} !('.css|'.js|'.png|'.jpg|'.gif|robots'.txt)$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2 [L,NC]

本教程非常有用。

相关文章: