html 分页的正确规则

问题描述

我已经重写了:

RewriteRule (.*\.html$) /index.PHP?$1 [L,QSA]

但有些页面分页,例如

/test.html?page=2

我需要将其重写为 test-page2.html

我试过了

RewriteRule (.*)-page([0-9]*)\.html$ /index.PHP?$1&page=$2 [NC,L]

但它不起作用

解决方法

使用您显示的示例,请尝试以下操作。在测试您的网址之前,请务必清除浏览器缓存。

RewriteEngine ON

##new rule added by me. This will check if a url has html as well as query string then it will rewrite it to html file.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.html\?(page)=(\d+)\s [NC]
RewriteRule ^  %1-%2%3.html [R=301,L]

##OP's previous rule.
##For safer side you could add an additional condition here too.
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*\.html)$ /index.php?$1 [L,QSA]
,

这对你有用吗?

RewriteEngine on

RewriteCond %{QUERY_STRING} ^([^&]+)&page=([0-9]+)$ [NC]
RewriteRule ^index\.php$  /%1-page%2.html? [L,R]
RewriteRule ^([^-]+)-page([0-9]+)\.html$ /index.php?$1&page=$2 [END]

请务必清除浏览器缓存或使用其他浏览器来测试此代码。