Ad

Seo Url Rewrite Rule - Make Last Parameter Optional

- 1 answer

This works but always expects the last parameter - I need 'page' to be optional for pagination purposes.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /catalogue.php?category=$1&page=$2 [L]

I have tried this from a previous stack post but it gives me a 404 error

Ad

Answer

You can use:

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/?$ /catalogue.php?category=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /catalogue.php?category=$1&page=$2 [L]

If it is Ok for you with empty page, and as you use the same page catalogue.php in both cases, you can use:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?([^/]*)$ /catalogue.php?category=$1&page=$2 [L]
Ad
source: stackoverflow.com
Ad