.htaccess rewrite for category with pagination

Viewed 176

I'm trying to write a mod-rewrite rule to handle pagination links on my site.

I'd like my URL structure to be this https://example.com/category.php?name=category-link to https://example.com/category/category-link [without pagination]

and https://example.com/category.php?name=category-link&page=2 to https://example.com/category/category-link/2 [with pagination]

I've tried the following:

RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+) category.php?name=$1&page=$2

https://example.com/category/category-link isn't working

404 page screenshot

https://example.com/category/category-link/1 is working

working page screenshot

2. am i able to redirect localhost/article/4/ to localhost/article/4 [here 4 is an id]

Any guidance would be much appreciated.

1 Answers

Could you please try following, based on your shown samples only. Please make sure you clear your browser cache after placing these rules into your htaccess file.

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/category/([0-9a-zA-Z-]+)/?$ [NC]
RewriteRule ^(.*)$ category.php?name=%1 [L]

RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]

OR you could use it without RewriteCond too. Make sure you are putting either of these NOT both of them please.

RewriteEngine ON
RewriteRule ^category/([0-9a-zA-Z-]+)/?$ category.php?name=$1 [L]
RewriteRule ^category/([0-9a-zA-Z-]+)/([0-9]+)/? category.php?name=$1&page=$2 [L]
Related