301 redirect subfolder without allowing subdirectories to redirect with same rule

Viewed 48

I have set up the 301 redirect below which will redirect /jobs and all subpages below it when a user navigates to

https://www.main-domain.com/jobs/

 RedirectMatch 301 /jobs/.* https://www.main-domain.com/all-vacancies/

My issue arises as I have a sub-site in a subdirectory on "main-domain.com" which also uses the path /jobs which is being redirected when I want it excluded e.g.

 https://www.main-domain.com/subsite/jobs/
 # I don't want this url redirected back to https://www.main-domain.com/all-vacancies/

I have tried the below to keep the /jobs to only redirect from the root of the domain but it did not work and I am stumped!

 RedirectMatch 301 ^(?i)/jobs/.*?$ https://www.main-domain.com/all-vacancies/

Cheers, JB.

1 Answers

You already match any request contains /jobs/ by your code but to achieve what you want , to only match request starts with jobs , you should do the following :

RedirectMatch 302 ^/jobs/.* https://www.main-domain.com/all-vacancies/

And also here this .* will not match any thong in new location and every request will be redirected to all-vacancies main page (index) but if you want any request to come as it is to all-vacancies change it to :

RedirectMatch 302 ^/jobs/(.*) https://www.main-domain.com/all-vacancies/$1

Note : , clear your browser cache and test it .if Ok , change 302 to 301

Related