Remove "page" from page url

I have a problem with changing pagination URLs in Wordpress. I know that a universal solution for this is to change the main Wordpress files, but I need this solution for only one category. Maybe for one category this can be done using htaccess?

Now there is a url: http://mysite.com/categoryname/ page / 3

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

Thanks for any answer

+4
source share
2 answers

You need to map to %{THE_REQUEST} to make sure that you match the actual request and not the internal rewritten URI:

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

They should be up to wordpress rules.

+3
source

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 will change your old categoryname/page/3 URL to categoryname/3 .
  • The second rule is an invisible redirect 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 a short version in the url.

Please note that you may experience problems with another similar URL (make sure you will never categoryname/3 elsewhere)

EDIT

Inifinite loop ... how could I miss it!

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

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

All Articles