301 redirects clashing possibly with CloudFlare - .htaccess

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.

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 redirect in htaccess doesn't work. What am I doing wrong?

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?

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

How to 301 redirect http:// to https:// for specific URLs (not typical example)

When editing the htaccess file...
How can you 301 redirect:
old url: http://example.com/old-long-url/
new url: https://example.com/new-url/
(note the difference in the protocol identifier, from http to https, for the target URL)
Every example I have found so far, does not allow for the start of the 301 code to target the http part.
Currently every http:// is redirected to https://... which means whenever I change a specific URL, I end up with a redirect chain.
E.G
http://example.com/old-url/
Redirects to..
https://example.com/old-url/
(due to the code that redirects every http to https)
And finally that gets redirected to:
https://example.com/new-url/
I hope that makes sense. It is confusing to me!

Redirect Old URL to New URL open cart using htaccess

I want to redirect 301 old url to new url.
my old url is;
http://www.domainname.com/special
and new url is;
http://www.domainname.com/offers
I tried this code:
redirect 301 /http://www.domainname.com/special http://www.domainname.com/offers
But still now it's not redirecting & taking me to the old URL which does'nt exist.
Try this :
Redirect 301 /special /offers
Second argument of Redirect Directive is the URL Path, it doesn't start with Scheme and hostname.
There are various ways to do this and various redirects, I've listed them below:
301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain:
This allows you to redirect your entire website to any other domain
Redirect 301 / http://example.com/
302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:
This allows you to redirect your entire website to any other domain
Redirect 302 / http://example.com/
Redirect index.html to a specific subfolder:
This allows you to redirect index.html to a specific subfolder
Redirect /index.html http://example.com/newdirectory/
Redirect an old file to a new file path:
Redirect old file path to new file path
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html
Redirect to a specific index page:
Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

Resources