如何重写分页网址?

时间:2016-06-10 08:21:39

标签: .htaccess mod-rewrite

我已使用以下代码将我的网页网址从localhost/products.php更改为localhost/products

RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]

但是当我导航到下一页时,地址栏会显示如下内容: localhost/products?page=2我希望显示为localhost/products/page2

试过这个RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L],但这不起作用。 我重写页面网址的完整代码是:

RewriteCond %{THE_REQUEST} /products\.php[\s/?] [NC]
RewriteRule ^ /products [R=301,L]
RewriteRule ^products/?$ products.php [L,NC]
RewriteRule ^products/page/([0-9]+)/?$ products.php?page=$1 [L]

在这里如何更改下一页的网址?我的意思是从localhost/products?page=2localhost/products/page2等等。

1 个答案:

答案 0 :(得分:0)

您可以使用以下规则:

RewriteCond %{THE_REQUEST} /products/?(?:\.php)?\?page=([^\s&]+) [NC]
RewriteRule ^ /products/%1? [R=301,L]
RewriteRule ^products/([0-9]+)/?$ /products.php?page=$1 [L,NC]

这将转换网址

  • /products.php?page=number
  • /产品/?页=数目

  • /产品/数