Home > OS >  Once set parent 301 redirection then child page redirection not working
Once set parent 301 redirection then child page redirection not working

Time:08-24

I am getting issues in 301 redirections

Example 1

I have a URL called https://www.example.com/abc/xyz-1 and I have to redirect to https://www.example.com/abc/xyz

So I have tried the below code and it's working.

Redirect 301 /abc/xyz-1 https://www.example.com/abc/xyz

Example 2

I have one more URL that I have to redirect

https://www.example.com/abc to https://www.example.com/postname and I tried the below code and this is also working.

Redirect 301 /abc https://www.example.com/postname

Now my issue is, that once I add the above code in my htaccess then the first example stops working.

Any idea how to solve this issue?

CodePudding user response:

Redirect module is usually the best choice for handling similarly matching URIs as it only matches starting part which is /abc in both the cases.

You should consider using RedirectMatch that supports regular expression for precise matching:

RedirectMatch 301 ^/abc/xyz-1/?$ /abc/xyz

RedirectMatch 301 ^/abc/?$ /postname

RedirectMatch 301 ^/job/?$ /opening/

Note use of regex anchors ^ and $ here for precise matching. e.g. ^/abc/?$ will match /abc or /abc/ but won't affect /abc/xyz-1.

  • Related