Paparappa
Paparappa

Reputation: 2189

htaccess rewrite folder with category and page id

I would like to be able to rewrite the url on my webpage so it also includes a category.

What I want is the URL to look something like this.

mypage.com/products/category/page

What I have now is a folder called products where I have an .htaccess file but I don't really understand how to use the variables to create something like above example. This is what I have right now.

RewriteEngine on
RewriteRule ^([a-z0-9-_]+)/?$^([a-z0-9-_]+)/?$ ../product.php?pageslug=$2&categoryslug=$1 [L]

Upvotes: 2

Views: 599

Answers (1)

anubhava
anubhava

Reputation: 785196

Your regex is not correct as you cannot use ^ and $ in the middle of your pattern.

You can use this rule inside products/.htaccess:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ /product.php?pageslug=$2&categoryslug=$1 [L,QSA]

This will rewrite

mypage.com/products/category/page

to this URL:

mypage.com/product.php?pageslug=page&categoryslug=page

If your product.php reside inside products folder then use rule as:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ product.php?pageslug=$2&categoryslug=$1 [L,QSA]

Upvotes: 1

Related Questions