htaccess multiple top level domains and http to https with www - .htaccess

I know the question is simple but I simply can not find it - so: I need to redirect all requested URIs to multiple top level domains, with or without www, with https or http to one single top level domain with https and www.
For example:
http://example.com
http://www.example.com
https://example.com
https://www.example.com
http://example.de
http://www.example.de
https://example.de
TO:
https://www.example.de
What does the correct rewrite block look like?

You may use this single rule for all these use-cases in your site root .htaccess:
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\.example\.de$ [NC,OR]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://www.example.de%{REQUEST_URI} [R=301,L,NE]

Related

http to https and non-www. to www. redirect HSTS header htaccess

I have a new website with an empty .htaccess file. I want to do the following:
redirect http to https
redirect non-www. to www.
I do not want to redirect it multiple times, just one redirect:
http://example.com should directly redirect to https://www.example.com.
Also, an HSTS header should be added.
Question: How can I arrange this in the .htaccess file.
Add these lines to the beginning of the .htaccess file where example.com is your domain.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
To set HSTS you can use this header:
Header set Strict-Transport-Security "max-age=31536000" env=HTTPS
Then to force HTTPs and WWW use the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your cache before you test this.
You can add to header "includeSubDomains" so redirect can be just http://example.com -> https://www.example.com.
This only have drawback that ALL subdomain need ssl enabled AND forced to use. Only if you planned not to use subdomains or they will have ssl properly configured
No, you cannot escape with 2 redirect.
Non www and www is interpreted as different host (domain and subdomain)
To correctly set it, you must first enforce https by HSTS and then you can redirect to proper one version. Example:
-- http://example.com -> https://example.com (HSTS loaded for this domain) -> redirect
-- http://www.example.com -> https://www.example.com (HSTS loaded for this domain) -> redirect
Source: https://www.sentinelstand.com/article/http-strict-transport-security-hsts-canonical-www-redirects

Redirect for non www to www and http to https

I am working on a redirect code (that will work) for my website (it is hosted on an Apache server). What I require is a code that will redirect ALL traffic from non www to www as well as All traffic from HTTP to HTTPS.
I have tried many codes but they either do not work fully or I get stuck on a redirect loop. So basically I want a code that will redirect:
http:// .example.com AND http://www.example.com AND https://.example.com
TO
https://www.example.com
Use this .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]

Rewrite all subdomains to HTTPS With A Few Twists

I scoured the digital realm trying to find a solution for this because it has to be a common scenario but, alas, I failed. I didn't want to paste my MULTIPLE ITERATIONS of HTACCESS blurbs. I promise, I exhausted the lot.
Short version: Forward all subdomains to HTTPS. If no subdomain is specified, forward to HTTPS WWW.
Detailed Version:
if http or https, if there is no subdomain, forward to https www.
i.e. http://example.com OR https://example.com
would forward to
https://www.example.com
if there IS a subdomain, then forward to the HTTPS for that subdomain.
i.e. http://www.example.com
would forward to
https://www.example.com
and any subdomain, same behavior
i.e. content.example.com
would forward to
https://content.example.com
You can use that:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirecting non www domain to www domain except https

i have a domain which which is like www.mydomain.com
and i want to redirect all non www request requests to www except for https requests.
i am using this one, which is redirecting all requests to www
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
what i want to do is, preventing redirects of requests like https://sslsites.com/mydomain.com
Add the following (before the RewriteRule):
RewriteCond %{HTTPS} off
Apache mod_rewrite documentation is available here.

How to redirect non-www to www with SSL for a specific domain not all parked domains

I have test.yoursite.com parked on example.com and have a SSL multi-domain certificate.
What I am asking is:
when http://example.com requested, .htaccess should redirect it to https://www.example.com;
when http://test.yoursite.com requested htaccess should redirect it to https://test.yoursite.com.
One of directions should work both with www and SSL redirection, another one is just for SSL redirection.
By the way I also have example.net parked on same host. I have tried several things which did not work. Waiting for your help.
Try
# Dealing with a domain with/without www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ https://www.%2.%3/$1 [L,R]
# Dealing with a domain that doesn't care about www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ https://%1.%2.%3/$1 [L,R]
The first rule will redirect http://www.example.com/something to https://www.example.com/something as well as http://example.net/foo to https://www.example.net/foo
The second rule will redirect hosts that don't care about the "www", such as http://subdomain.domain.com/bar to https://subdomain.domain.com/bar
It will not, however, redirect http://sub-sub.subdomain.domain.com/.

Resources