Remove “page” from pagination URL

白昼怎懂夜的黑 提交于 2019-12-07 09:06:01

问题


I have problem with changing pagination URL in Wordpress. I know that universal solution for this is to changing Wordpress core files, but I need this solution only for one category. Maybe for only one category this can be done by htaccess?

There is now URL like this: http://mysite.com/categoryname/page/3

and I want change it to this: http://mysite.com/categoryname/3

Thanks for any response


回答1:


You need to match against the %{THE_REQUEST} to ensure that you're matching against the actual request and not an internally rewritten URI:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /categoryname/page/([0-9]+)
RewriteRule ^ /categoryname/%1 [L,R=301]

RewriteRule ^/categoryname/([0-9]+)$ /categoryname/page/$1 [L]

These have to be before the wordpress rules.




回答2:


Basically, you should use this htaccess rule :

RewriteRule ^categoryname/page/([0-9]+)$ /categoryname/$1 [L,R=301]
RewriteRule ^categoryname/([0-9]+)$ /categoryname/page/$1 [L]
  • The first rule change your old url categoryname/page/3 into categoryname/3.
  • The second rule is an invisible redirection from categoryname/3 to categoryname/page/3.

Then you can use both categoryname/page/3 and categoryname/3 in your code, this will always show the short version in url.

Notice that there could have some issues with other similar url (make sure that you'll never user categoryname/3 somewhere else)

EDIT

Inifinite loop... how could I miss it !

The way to avoid this loop will be to find the "final url" for category/page/3 (for example : index.php?category_name=categoryname&page=3), and use it in your second rule :

RewriteRule ^categoryname/page/([0-9]+)$ /categoryname/$1 [L,R=301]
RewriteRule ^categoryname/([0-9]+)$ /index.php?category_name=categoryname&page=$1 [L]


来源:https://stackoverflow.com/questions/12050524/remove-page-from-pagination-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!