首页 文章

.htaccess重写规则,在URL末尾添加html

提问于
浏览
0

我的htaccess文件中有以下规则:

RewriteRule shop/hats/([0-9]+)/ shop/item.php?id=$1

这将重写一个URL,如:

http://www.example.com/shop/item.php?id=3

至:

http://www.example.com/shop/hats/2/

我的问题是,如何修改它以重写URL:

http://www.example.com/shop/hats/2.html

2 回答

  • 0

    $1 只是表示括号中的匹配模式 - 如果你有其他集合,你可以使用 $2$3 等 . 所以 $1.html 而不是 $1 应该可以解决问题 .

    编辑:如果您的示例网址正确,则您的规则非常错误 . 尝试这样的事情?

    RewriteCond %{QUERY_STRING} id=(\d+)
    RewriteRule ^/shop/item.php /shop/hats/%1.html? [R=301]
    

    您需要使用RewriteCond来匹配查询字符串,而 %1 而不是 $1 来引用该匹配 .

  • 0

    您只需要在重写规则中进行一些小调整:

    RewriteRule ^shop/hats/([0-9]+)\.html$ shop/item.php?id=$1 [NC,L,QSA]
    

相关问题