htaccess URL rewrite page.php?page=string containing a hyphen

Viewed 87

I need a solution for my redirect problem. In .htaccess file I have this code:

RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+) category.php?page=$1 [NC,L,QSA]

And this is working for a query like category.php?page=pinturas and redirect to category/pinturas.

But, if the string have - like this: category.php?page=aquoso-madeira it redirects to category/aquoso

Any help?

1 Answers

category.php?page=aquoso-madeira It redirect to category/aquoso

Because your RewriteRule pattern does not include the hyphen (-), so it matches everything up to, but excluding, the first hyphen.

Include the hyphen in the character class:

RewriteRule ^category/([a-zA-Z0-9-]+) category.php?page=$1 [NC,L,QSA]

Note that the hyphen (-) must go at the start or end of the character class, since this is a special character and has alternative meaning when used elsewhere.

And this working for query like category.php?page=pinturas and redirect to category/pinturas

Note that this rewrite does the complete opposite of what you are describing.

This rule takes a request like category/pinturas and internally rewrites it to category.php?page=pinturas. (There is no external redirect here - which might be in another part of your code?)

Related