How can i do the dynamic .htaccess redirection on multiple directories level

Viewed 40

I have multiple cities directories, want to do the 301 internal redirections for many cities.

e.g. https://wwww.exampl.com/ciytname/abc/initial_url.html want to redirect on https://wwww.exampl.com/cityname/abc/final_url.html

cityname is variable

my code below not working out.

Redirect 301 /*/abc/initial_url.html /*/abc/final.html

Redirect 301 /cityname/abc/initial_url.html /cityname/abc/final.html

I can't do for each city

2 Answers

You may use a RedirectMatch rule that supports regular expressions like this:

RedirectMatch 301 ^/([^/]+/abc)/initial_url\.html$ /$1/final_url.html

For your shown samples, could you please try following.

RewriteEngine ON
RewriteRule ^ciytname/abc/initial_url.html/?$ cityname/abc/final_url.html [NC,L]


For any generic URIs could you please try following.

RewriteEngine ON
RewriteRule ^([\w-]+)/([\w-]+)/initial_url.html/?$ $1/$2/final_url.html [NC,L]
Related