Redirect combinations in htaccess - .htaccess

I have my domain with a ssl certificate, I want to redirect everything to use https://www.example.com (with https and www)
This is what I have so far:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
However, this two combinations don't work and shows apache2 default page:
http://example.com
http://www.example.com
what am I missing?

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
# line above to catch any request with http regradless of having www or not
RewriteCond %{HTTP_HOST} !^www\.
# line above to catch any request with https and without www
RewriteRule ^(.*)$ https://www.example.com/$1 [R=302,L,NE]
So , any request with (http or https) and (www or none www) will be redirected to https://www.example.com
Note: clear your browser cache and test it , if Ok , change 302 to 301 to get permanent redirection

Related

redirect everything to https://www for main domain except for for subdomains

I want these redirects
http://example.com => https://www.example.com
http://www.example.com => https://www.example.com
https://example.com => https://www.example.com
https://example.com/asa.php => https://www.example.com/asa.php
http://subdomain.example.com => https://subdomain.example.com
https://www.subdomain.example.com => https://subdomain.example.com
What I have tried is
#redirect http or https to https//www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But these rules handle all domains of the site, not just the main site.
The idea is to make all the incoming http to https.
And all main site non-www to www.
And for subdomain from www to non-www if any.
All in all, these are three separate requests.
change http to https, this should already work as is
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
redirect main site example.com to www.example.com. This can be done by being more specific
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R,L]
redirect non main sites from www.subdomain.example.com to subdomain.example.com. To strip the leading www, you can capture the part after www., and use the captured subdomain in the rewrite rule
RewriteCond %{HTTP_HOST} ^www\.(.+\.example\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
Finally, put 1. at the end to avoid two redirects for e.g. http://example.com or http://www.subdomain.example.com
Unrelated, but never test with R=301!

Redirect [http and/or non-www] to [https://www.] in ONE redirect

I have setup redirects in the htaccess file. It redirects all non-https and non-www requests to https-www versions.
Following are the rules I have used in the .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The redirect works fine. My only problem here is that it requires 2 redirects. Let's say my website is example.com.
When I open example.com, it first redirects to https://example.com and then it gets redirected to https://www.example.com
So, I get the response code after two redirects. 301-301-200
Now my question is: Is there any way to have it done in a single redirect?

Redirect to non www is not working for pages in https

I am trying to add redirect rules using .htaccess for such goals:
Redirect all http pages to https.
Redirect all www http and https pages to non www https.
My .htaccess code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Everything is working good except one:
http redirects to https (all pages)
https www redirects to https non www (main page and subfolders)
But https://www.example.com/1/page.html does not redirect to https://example.com/1/page.html (both pages open)
What is the problem? How to write an .htaccess rule to redirect all the pages to https non www?
You can use this to remove www and force https:
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://example.com%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your cache before testing this. Combing the two rules will help to speed up your website too, instead of having to run two seperate rules.

How to redirect https://www to https:// and http://www to https:// and http:// to https://

I need to redirect all the following addresses :
http://www.example.com
https://www.example.com
http://example.com
to https://example.com using a permanent redirect in an .htaccess file.
I'd try the following:
RewriteEngine On
# URL with www rewrite to https without www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
# URL without www rewrite to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

How to get htaccess redirect for non-www to https?

I would like to have an htaccess that does the following:
http://example.com redirect to: https://www.example.com
https://example.com redirect to: https://www.example.com
example.com redirect to https://www.example.com
www.example.com redirect to https://www.example.com
So basically every method of the url redirects to https://www.example.com
Thanks.
I used this but its not working for all instances above:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Change your rules to this:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
and make sure to clear your browser's cache before retesting.

Resources