RewriteRule with dash or hyphen

Viewed 154

In accordance with the SEO guidelines I am changing pages url from

website.com/color_red/ (underscore) to

website.com/color-red/ (dash / hyphen)

/color-red/ must simply produce ?c=red

In .htaccess this works perfectly with underscore:

RewriteRule ^color_([^/\.]+)/?$ ?c=$1 [L]

not with dash:

RewriteRule ^color-([^/\.]+)/?$ ?c=$1 [L]

I think dash char must be escaped but I don't know how.

2 Answers

Could you please try following. Please make sure to clear your browser cache before testing your URLs.

1st solution: To handle both _ OR - in your URLs then try following.

RewriteEngine ON
RewriteRule ^color[_-]([^/]*)/?$ ?c=$1 [L]

2nd solution: In case you only want to handle - in your URLs then try following.

RewriteEngine ON
RewriteRule ^color-([^/]*)/?$ ?c=$1 [L]

A simple solution is to escape the hypen/dash char - with square brackets [-] :

RewriteRule ^color[-]([^/\.]+)/?$ ?c=$1 [L]
Related