STATWORX
STATWORX

Reputation: 11

htaccess Redirect 301 redirects to wrong url

I've created a bunch of 301 redirects in my .htaccess, for example

Redirect 301 / /de/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/

and so on.

When I enter

www.old.com/site_1/ (wrong)

I get directed to

https://www.new.com/de/site_1/ (services folder missing)

It seems that the parent folder is missing in the redirected URL. Same for all other sites that reside in folders.

Upvotes: 1

Views: 85

Answers (1)

MrWhite
MrWhite

Reputation: 45829

Redirect 301 / /de/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/

Since the mod_alias Redirect directive is prefix-matching, a request for www.old.com/site_1/ would actually get caught by your first (most general) rule. And everything after the match (ie. site_1/) gets appended onto the end of the target URL (ie. /de/), so the resulting redirect becomes /de/site_1/ (but not to new.com as you've stated?).

You could resolve this by reversing the directives, to have the most specific matches first. For example:

Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 / /de/

Or, as you mentioned in comments, use RedirectMatch instead - which is not prefix-matching and matches against a specific regex instead. Although you will still need to modify the pattern. Something like:

RedirectMatch 301 ^/$ /de/
RedirectMatch 301 ^/site_1/$ https://www.new.com/de/company/site_1/
RedirectMatch 301 ^/services/site_2/$ https://www.new.com/de/services/site_1/

Although this now matches the exact URL, which may or may not be what you require.

Upvotes: 1

Related Questions