Right rule for html pagination

Viewed 49

I have rewrite:

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

but some pages have pagination, for example

/test.html?page=2

i need to rewrite it to test-page2.html

i tried

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

but it is not working

2 Answers

With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.

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]

Does this work for you?

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]

Make sure to clear your browser cache or use different browser to test this code.

Related