Migrate one domain to another and force https on new domain - apache

I am trying to migrate one domain to a new domain as well as force everything to https.
I have tried a few iterations using .htaccess with no luck.
One thing I tried was:
RewriteCond %{HTTP_HOST} !newsite.com$ [NC]
RewriteRule ^(.*)$ https://www.newsite.com/$1 [L,R=301]
This redirects everything except the oldsite https links.
Also have tried other options like:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.oldsite.com\.com$ [NC]
RewriteRule (.*) https://newsite.com/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} !newsite.com$ [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]
Also with no luck.
I imagine I am missing something quite simple...any tips would be appreciated!

Besides unescaped . character, your initial attempt is correct. To redirect one domain to another and switch to HTTPS connection, considering that there are other sites pointing to the same document root:
RewriteEngine on
RewriteCond %{HTTP_HOST} !newsite\.com$ [NC]
RewriteRule (.*) https://www.newsite.com/$1 [R=301,L]
UPDATE:
Although the rule above is correct, redirection from https://www.oldsite.com to https://www.newsite.com didn't occur for original poster because newsite.com didn't have a valid certificate.

Related

Htaccess RewriteCond Expr not executing

Per Amazon's Lightsail Troubleshooting I'm trying to update my htaccess to ignore a specific file during the HTTPS redirecting. However, it doesn't seem to be ignoring it.
What I want to happen is, if the user visits http://example.com/health-checker.html they aren't redirected to the HTTPs version. However, if they visit anything else they are redirected to the HTTPs version.
When I use the below code, they are still directed to the HTTPS version. To test if it was a browser caching issue, I used a different file name and still same problem.
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond expr "! %{REQUEST_URI} -strmatch '*health-checker.html'"
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
Please note that I'm hard coding my domain name intentionally due to dealing with multiple domain names.
Your assistance is appreciated
You don't really need expr here, you can just use regular RewriteCond with negation like this:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{THE_REQUEST} !/health-checker\.html [NC]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE]

Simple htaccess mod rewrite

I have multiple domains that end with .nl .com and .de. For each domain name I have a different language. My files are on the .nl domain. What I would like is the following for all domains:
Redirect http://* to https://*
Redirect http://example.* to https://www.example.*
I Have the files on 1 server, and want them to look like it is on 3.
This is what I have on the main server (.NL)
RewriteCond %{HTTP_HOST} !www.example.nl$ [NC]
RewriteRule ^(.*)$ https://www.example.nl/$1 [L,R=301]
And this for the .DE
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.de/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !www.example.de$ [NC]
RewriteRule ^(.*)$ http://www.example.de/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.nl/$1 [P]
And this for the .COM
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.nl/$1 [P]
I've tried everything, but I could not figure it out.
# This rule will redirect users from their original location, to the same location but using HTTPS.
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.example.nl/$1 [R=301,L]
#primary site redirect
RewriteCond %{HTTP_HOST} !^www\.example\.nl [NC]
RewriteRule (.*) https://www.example.nl/$1 [R=301,L]
I believe that's all that's required, as you can see, you first redirect ALL requests that are not https to https example.nl, then you redirect anything that is NOT www.example.nl to https://www.example.nl
If I'm guessing wrong about what you need with [P] you can just replace I think [R=301,L] with [P], though I've never dealt with that configuration so I can't say for sure, but it's easy to test.
https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
this is more or less what we use for https. I think you're getting confused and not realizing that all you need to know is that something is NOT example.nl/$1, there's no need to list all those nots, since all of them are not example.nl/$1
Note the following, which are all NOT the second rule, all will get redirected:
https://example.nl
https://www.example.com
https://example.de
https://www.example.de
Since these are all not:
https://www.example.nl
they get redirected. (.*) means everything, including /.
There's a subtle thing that you need to be aware of with https, you have to have a certificate for ALL versions of all the domains, that's www/non www, or you get those browser alert popups for any initial domain/subdomain that does not have certificate support when the page initially loads. For example, if you don't have a certificate for example.com but you have one for www.example.com if a url comes in: http://example.com you'll get that browser alert, which is very bad for usability and makes it look like it's a scammer site.
I can't remember the exact ordering of processing, but I think that's how it works.

.htaccess redirect all tld's to another and force https

Hello i try to redirect all my top level domains to another one and this is already working but i also want to force https. With the following code it is working fine for all top level domains such as '.de', '.net' and so on.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]
My problem is i also want to force https for my main tld => '.com'
Well, and here we are:
RewriteEngine On
RewriteCond (http://) !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]
For my bad, this is not working. I get an 'Too many redirects' error. My question is now is it the code/redirection or is there something else wrong maybe with the server configuration of my provider? I noticed the 'Too many redirects' error in any way i tried till now for redirect from 'http://' to 'https://' and i tried so many different ways... .
Try:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]
Adds an "or" condition so that it redirects if there's no HTTPS OR if the domain is different.
The (http://) isn't a variable, it's a literal "http://", which will obviously never match ^(www\.)?domain\.com$, which is why that condition is always true.
Since you're using cloudflare, which internally routes requests (that are not HTTPS), you can't use the %{HTTPS} variable, you need to look at the forwarding protocol:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*) https://domain.com/$1 [R=301,NE,L]

.htaccess file making new domain redirect to my original domain

To give a little background that may help understand this. I only had one domain on my host's server initially. I just added a new domain and it was redirecting to my first site.
I had to create an .htaccess file redirect my first site so that it would have my SSL on every page and so that it took the www. away from the sites name. The www. was causing issues with sessions when a customer would login.
So my code was like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
The tech support of my host company said that this line was making my new site redirect to my old site:
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
How can I make my code work so that is still has the exact same functionality of what it was doing and not interfere with my new domain and make it re-route to my old site?
Why not just exclude the new site, if I am understanding correctly?
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?newsite\.com [NC]
RewriteCond %{HTTP_HOST} ^www\.oldsite\.com [NC]
RewriteRule ^(.*)$ https://oldsite.com/$1 [R=301,NE,L]
RewriteCond %{HTTPS} ^off
RewriteCond %{HTTP_HOST} !^(www\.)?newsite\.com [NC]
RewriteRule ^(.*) https://oldsite.com/$1 [R=301,NE,L]
Alternatively maybe you can just tell it to not do anything based on the host. Try putting this at the top of your rules.
RewriteCond %{HTTP_HOST} ^(www\.)?newsite\.com [NC]
RewriteRule ^ - [L]
Note I haven't tested this.

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]