301 redirect in htaccess doesn't work. What am I doing wrong? - .htaccess

I've made an htaccess file, but several testers tell me it's not right.
Domain B is going offline and I want to redirect all the pages to new pages on domain A.
This is the code:
redirect 301 / https://domain-A.com/
redirect 301 /page-1/domain-B/ https://domain-A.com/page-1/
redirect 301 /page-2/domain-B/ https://domain-A.com/page-2/

redirect 301 / https://domain-A.com/
redirect 301 /page-1/domain-B/ https://domain-A.com/page-1/
redirect 301 /page-2/domain-B/ https://domain-A.com/page-2/
Your rules are in the wrong order. The Redirect directive is prefix-matching. The first rule redirects everything (and preserves the URL-path). The second and third rules are never processed. (What exactly is the "tester" reporting? Is this a thrid party tool or a real "tester" person?)
If you request /page-1/domain-B/ you will see that you are not redirected as intended. (You are redirected to https://domain-A.com/page-1/domain-B/, not https://domain-A.com/page-1/ as would seem to be the intention.)
You need to reverse the order of these rules. The most specific needs to be first.
For example:
Redirect 301 /page-1/domain-B/ https://domain-A.com/page-1/
Redirect 301 /page-2/domain-B/ https://domain-A.com/page-2/
Redirect 301 / https://domain-A.com/
You will need to clear your browser cache before testing since the erroneous 301s will have been cached by the browser. Test first with 302 redirects to avoid caching issues.
/page-1/domain-B/
And /domain-B/ is actually in the URL-path?

Related

Htaccess redirect only top level URL

I would like to redirect a top level page using htaccess, but not redirect tier pages. Let me explain. I currently have this redirect in place:
Redirect 301 /support /donate
In summary, I want someone to be redirected to /donate when the visit /support. However, with this rule if someone visits:
https://www.example.com/support/test
They are redirected to:
https://www.example.com/donate/test
I do NOT want them to be redirected in these instances - only if they visit /support or /support/ (note trailing slash).
I'm not sure how to do this or if this is possible. Any ideas?
The Redirect directive is prefix-matching, so you will need to use the RedirectMatch directive instead, which matches against a regex.
For example:
RedirectMatch 301 ^/support/?$ /donate
The above matches /support or /support/ only and redirects to /donate in both cases. Note that the previous Redirect directive would have redirected to /donate or /donate/ depending on whether there was a trailing slash on the initial request.
You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirects will have been cached persistently by the browser. Test first with 302s to avoid potential caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectmatch

301 redirects clashing possibly with CloudFlare

My old 301 redirects in .htaccess are no longer working because something is redirecting things twice. Example of redirect:
Redirect 301 /oldpage.html //www.example.com/newpage.php
This used to work, but now it goes to:
https://www.example.com//www.example.com/newpage.php
In CloudFlare, I have a Page Rule for the www redirection:
https://example.com/*
Forwarding URL (Status Code: 301 - Permanent Redirect, Url: https://www.example.com/$1)
And my "Always Use HTTPS" in CloudFlare is on. What could be causing the double redirect?
I put one of the URLs into a redirect checker http://www.redirect-checker.org/
and noticed that for some reason the URLs go back to http, then back to https again. So I went back and edited my .htaccess file to explicitly include the https:
Redirect 301 /oldpage.html https://www.example.com/newpage.php
And now the redirect works as expected, at least in the incognito window. The regular browser isn't yet reflecting the change.

htaccess Redirect 301 is chaining itself

I have around 100+- redirects in a .htaccess file and some urls are chaining each other.
For example:
Redirect 301 /air-india-trial/air-india-promo-conditions/ /features/
Goes to
/trial/air-india-promo-conditions/
Because of
Redirect 301 /air-india-trial/ /trial/
So the URL is replacing '/air-india-trial/' for '/trial/' because of the 2nd redirect being called. I already tried to put https://example.com/ before the 2nd URL in the Redirect 301 but that doesn't work. Not sure if it matters but the URL /air-india-trial/ doesn't exist on the new website. The domain is the same as the old website tho. Anyone that has an idea to fix those redirect chains?
Redirect 301 /air-india-trial/air-india-promo-conditions/ /features/
Goes to
/trial/air-india-promo-conditions/
Presumably you mean a request for /air-india-trial/air-india-promo-conditions/ ends up being redirected to /trial/air-india-promo-conditions/ (that directive doesn't "go to" anything).
...because of the 2nd redirect being called. I already tried to put https://example.com/ before the 2nd url in the Redirect 301 but that doesn't work.
You can't put https://example.com/ as part of the URL in the first argument - if that is what you are referring to? It simply won't match. It matches against the URL-path only.
Since the Redirect directive is prefix-matching (and everything after the match is copied onto the end of the target URL), you need to order your Redirect directives in order of specificity. The most specific (ie. longest path) redirect needs to be before the less specific redirects.
mod_alias Redirect directives do not "chain" together as you seem to imply.
So, in your example, the following should work to redirect /air-india-trial/air-india-promo-conditions/ to /features/:
Redirect 301 /air-india-trial/air-india-promo-conditions/ /features/
Redirect 301 /air-india-trial/ /trial/
(Although you do perhaps suggest that the directives are already in order (or are you just referring to the order in your question)? In which case there is still a conflict with another directive or you are seeing a cached response.)
You will need to clear your browser cache after making this change as the 301 (permanent) redirect will be cached by the browser.
If you specifically want to redirect only /air-india-trial/ and not /air-india-trial/<something> then you need to use a RedirectMatch directive instead, which matches against a regex and is not prefix-matching. For example:
RedirectMatch 301 ^/air-india-trial/$ /trial/
Also, if you have any mod_rewrite (RewriteRule) redirects then these might conflict. Since different Apache modules runs independently and at different times throughout the request, you should avoid mixing redirects from both modules because of potential conflicts. (mod_rewrite will always run first on Apache 2.4, despite the apparent order of these directives in .htaccess.)

301 redirect from one domain to another but only for specific pages

We are rebuilding a website for a client from ASP to WordPress. This website will have a different domain, url structure, and file extension. I am only just getting my head around htaccess 301 redirects, and I know enough that I can't do the following:
Redirect 301 http://www.site1.com/about_us.asp https://site2.com/about/
Redirect 301 http://www.site1.com/art-specs/ https://site2.com/specs/
Redirect 301 http://www.site1.com/page/product1/ https://site2.com/product1/
There are about 12 links in total that need to be redirected, and I want to make sure that it is done right the first time as a client's SEO rankings are on the line.
Is there a variation of the above format that I could use? Or a rewrite rule that needs to be done first? Any help (and explanations) would be greatly appreciated!
After looking more into it, I realised that the htaccess file shouldn't need anything other than relative access to the original domain.
i.e. You shouldn't need to declare: http://www.site1.com/about_us.asp since the server and domain should be configured in such a way that /about_us.asp means the same thing.
So the correct answer would be to:
[1] Configure the server (in my case cPanel) by having the original domain added as an addon domain (e.g http://www.site1.com/).
[2] In the htaccess file I would add each of the 301 redirects to the htaccess file:
Redirect 301 /about_us.asp https://site2.com/about/
Redirect 301 /art-specs/ https://site2.com/specs/
Redirect 301 /page/product1/ https://site2.com/product1/
...for each redirect
[3] And finally, adding the following to the bottom of the htaccess file will catch everything else and redirect them to the home page:
RedirectMatch 301 .* https://site2.com

.htaccess Redirect 301 Issue

I am having an htaccess issue that I cannot seem to figure out.
On my site I have several pages that have /wiget/ in the url.
.com/wiget/
.com/wigeta/
.com/wigeb/
.com/wigetc/
.com/shop/wigeta/
.com/shop/wigetb/
In my htaccess file I am using the following:
Redirect 301 /wiget/ http://www.site.com/content/wiget/
Redirect 301 /wigeta/ http://www.site.com/content/wigeta/
Redirect 301 /wigetb/ http://www.site.com/content/wigetb/
Etc.
What is happening, is that every URL with /wiget/ /wigeta/ /wigetb/ in it is getting redirected incorrectly. For example, these urls:
.com/shop/wiget/ is being sent to .com/shop/content/wiget/
.com/shop/wigeta/ is being sent to .com/shop/content/wigeta/
.com/shop/wigetb/ is being sent to .com/shop/content/wigetb/
What I want is only pages that have .com/wiget/ or .com/wigeta/ or .com/wigetb/ to be redirected to their .com/content/wiget{x}/ page
I am a rookie at htaccess and I cannot drum up the solution.
So basically you want to specify that your url have to start with /wiget/
RewriteRule ^/wiget/ /content/wiget/ [L,R=301]

Resources