htaccess Redirect 301 redirects to wrong url - .htaccess

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.

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.

Related

301 Redirection for any url in my website

I need to create a 301 redirection from myOldSite.com to myNewSite.com
I have this code in the htaccess in myOldSite.com
#Redirect 301 / https://myNewSite.com/myPage/
This works fine to redirect myOldSite.com but it does not work to fire the right redirection for myOldSite.com/services.html:
myOldSite.com is properly redirected to myNewSite.com/myPage
But
myOldSite.com/services is redirected to myNewSite.com/myPage/services.html This is wrong.
As you can see servicesis appended to the end. This is not what I need. I need:
myOldSite.com/services to be redirected to myNewSite.com/myPage
Any url which start with myOldSite.com/ANYTHING_HERE should be redirected to myNewSite.com/myPage
Use RedirectMatch
RedirectMatch 301 / https://myNewSite.com/myPage
This will not append the old URL subpath to the destination URL.

301 Redirect in .htaccess is appending old file path/name to new url

I have two URLs: old-site.com and new-site.com.
My root folder on the old-site.com has NO FILES in it but only the .htaccess file. I don’t want to use the old domain anymore. However, I do want to pass the link juice to the new domain. Therefore, I created 301 Redirects in the .htaccess file and put it in the root folder on the old-site.com.
Why would the Redirect 301 append the “old-site.com/…” to the new-site.com?
My entire .htaccess looks like this (I skipped a few links to shorten it):
#Begin 301 Redirects
Redirect 301 / https://www.new-site.com/
Redirect 301 /contactus https://www.new-site/contact-us/
Redirect 301 /rentals https://www.new-site/lodging/
Redirect 301 /lift.html https://www.new-site/our-rentals/boat/
Redirect 301 /rentals.html https://www.new-site/our-rentals/
Redirect 301 /map.html https://www.new-site/contact-us/
Redirect 301 /giftshop https://www.new-site/store/gift-shop/
#End 301 Redirects
I don’t have any Rewrites. The above is my entire code.
The following redirect works fine:
Redirect 301 / https://www.new-site.com/
However, any other redirect creates the following absolute path on the new-site.com with a 404 error:
If I redirect:
Redirect 301 /contactus https://www.new-site/contact-us/
It goes to:
https://www.new-site.com/old-site.com/contactus
or
If I redirect:
Redirect 301 /lift.html https://www.new-site/lift/
It goes to:
https://www.new-site.com/old-site.com/lift.html
Why would the Redirect 301 append the “old-site.com/…” to the new-site.com?
Thank you,
Derek
Your rules will not do what you need correct because of this line Redirect 301 / https://www.new-site.com/ which will match any request first and if you put it in the last it will also match any request so , if you want it to match only root use RedirectMatch to be able to use regex like this :
RedirectMatch 301 /?$ https://www.new-site.com/
By this the rest of rules will work as expected .
Note: clear browser cache the test

301 Redirect just first Subfolder

I have a 301 Redirect from one page to another
REDIRECT 301 /cloud-computing /it-infrastructure/cloud-computing
Now when i use this redirect also the subpages of cloud-computing are affected by this 301 redirect, but they have to be redirected somewhere else. How can i just redirect the folder and not the subpages?
You should be using RedirectMatch for precise matching using regex:
RedirectMatch 301 ^/cloud-computing/?$ /it-infrastructure/cloud-computing
Clear your browser cache before testing the change.

Using .htaccess to redirect to a subfolder

I'm trying to 301 redirect from '/en' or '/en/' to '/en/home' using .htaccess, but any attempt I do results into a redirection loop '/en/home/home/home/home/home/home...'.Shouldn't it be as simple as Redirect 301 /en /en/home?
Redirect based rule keep matching /en in redirected URL as well. You can use RedirectMatch for this with regex support:
RedirectMatch 301 ^/(en)/?$ /$1/home
Also make sure to clear your browser cache when you test this.
You have to use the full URL, example:
redirect 301 /folder_wrong/name.html http://website.com/folder-right/name.html

Order of 301 Redirects?

I am curious if someone could educate me to better understand why the following does not work based on the order.
When I have the redirect for /contact first the location pages fail to redirect properly.
Redirect 301 /contact http://www.example.com/contact-us
Redirect 301 /index.php/contact/location1 http://www.example.com/contact-us/location1
Redirect 301 /index.php/contact/location2 http://www.example.com/contact-us/location1
When I have it after the locations, they work normally. Why is this?
Redirect 301 /index.php/contact/location1 http://www.example.com/contact-us/location1
Redirect 301 /index.php/contact/location2 http://www.example.com/contact-us/location1
Redirect 301 /contact http://www.example.com/contact-us
It is because other 2 URLs also have /contact in them.
It is always better to use RedirectMatchdirective that with capability to use regex so that you can match exactly what you need.
Using RedirectMatchdirective following will also work:
RedirectMatch 301 ^/contact/?$ http://www.example.com/contact-us
RedirectMatch 301 ^/index\.php/contact/location1/?$ http://www.example.com/contact-us/location1
RedirectMatch 301 ^/index\.php/contact/location2/?$ http://www.example.com/contact-us/location1

Resources