Order of 301 Redirects? - .htaccess

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

Related

Problem with path 301 redirects in htaccess

We're trying to redirect a bunch of category URLs like this:
Redirect 301 /table/ https://www.example.com/abc/
Redirect 301 /table/accessoires/ https://www.example.com/def/
Redirect 301 /table/accessoires/tablecloth/ https://www.example.com/ghj/
The first redirect works correctly. But why do the other 2 don't work?
Order matters. The more specific rules should be first.
Redirect 301 /table/accessoires/tablecloth/ https://www.example.com/ghj/
Redirect 301 /table/accessoires/ https://www.example.com/def
Redirect 301 /table/ https://www.example.com/abc/

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.
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.

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

Very Simple 301 Redirects First line only working in htaccess

Have searched high and low for an answer - 301 redirects seem a common issue on here but after much reading I'm stumped!
I have a simple htaccess file with three 301 redirects in:
redirect 301 example1.htm http://www.example.co.uk/newexample1.htm
redirect 301 example2.htm http://www.example.co.uk/newexample2.htm
redirect 301 example3.htm http://www.example.co.uk/newexample3.htm
However, only the first 301 redirect is working. Subsequent redirects aren't being followed.
The site doesn't have a CMS - is all just pure css/html.
Any suggestions?
Try these rules with RedirectMatch to target specific page with regex:
RedirectMatch 301 ^/example1\.htm$ http://www.example.co.uk/newexample1.htm
RedirectMatch 301 ^/example2\.htm$ http://www.example.co.uk/newexample2.htm
RedirectMatch 301 ^/example3\.htm$ http://www.example.co.uk/newexample3.htm

Resources