vanurag
vanurag

Reputation: 297

htaccess redirect pattern not working

I am using the following code in my htaccess file

Redirect 301 /([0-9]{1,2})+_([A-Za-z])$ /brand/$1-$2

to redirect the following type of URL pattern :

www.mywebsite.com/{ID}_{BrandName}

So basically I have to redirect

www.mywebsite.com/{ID}_{BrandName} to www.mywebsite.com/brand/{ID}-{BrandName}.

For example : Redirect 301 to www.mywebsite.com/5_TestBrand to www.mywebsite.com/brand/5-TestBrand.

Please suggest.

Below is the htaccess code which is not working :

Redirect 301 /([0-9]{1,2})+_([A-Za-z])$ /brand/$1-$2

Please Guide.

Thanks !

Upvotes: 3

Views: 103

Answers (2)

anubhava
anubhava

Reputation: 785128

You cannot use regex and captured groups in Redirect directive. Use RediectMatch instead:

RedirectMatch 301 ^/([0-9]+)_([A-Za-z]+)/?$ /brand/$1-$2

Upvotes: 3

Kevin
Kevin

Reputation: 1725

Make sure the rewrite engine is on at the top of your htaccess.

RewriteEngine on

Then, shouldn't it look something more like this:

RewriteEngine on
RewriteRule ^(.*)/([0-9]+)_([A-Za-z])$ /$1/brand/$2-$3 [R=301,NC,NE,L]

Or perhaps:

RewriteEngine on
RewriteRule ^([0-9]+)_([A-Za-z])$ /brand/$1-$2 [R=301,NC,NE,L]

Something like that, I always have to fiddle around with the htaccess to get it perfect.

Upvotes: 0

Related Questions