Rewrite rule with SSL not working - .htaccess

I want to redirect https://example.com to https://www.example.com
And if I go to https://example.com/aboutus,
it should also redirect to https://www.example.com/aboutus.
Currently, I have this in .htaccess but it make ssl certificate disappear.
RewriteCond %{HTTPS} off
RewriteCond %{DOCUMENT_ROOT}/../.force_ssl -f
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Just try this rule at top of your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Make sure to clear your browser cache.

Related

Redirect http, https to https://www.domain-name

I got www ssl certificate. I've to redirect my site https://www.domain-name.com/folder/home.php whenever user typing like http://domain-name.com or https://domain-name.com or domain-name.com
I tried many ways. It's working when I type domain-name.com but it shows privacy error and not redirect while using https://domain-name.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
Please change
RewriteRule .* https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
To
RewriteRule ^(.*)$ https://www.%{SERVER_NAME}%{REQUEST_URI} [R,L]
Try this one:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

htaccess redirect avoid specific domains

RewriteCond %{HTTP_HOST} !^(www\.)?(domaintoavoid1|domaintoavoid)\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(www\.)?(domaintoavoid1|domaintoavoid2)\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https%1://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I currently have this www/https redirection, but I have certain domains on which I didn't set up a SSL certificate and I'd like to be able to bypass the redirect entirely. It isn't currently working though, domaintoavoid.com and www.domaintoavoid.com still get redirected to https.
Have it like this in separate rules
# skip these domains from https/www rule
RewriteCond %{HTTP_HOST} ^(?:www\.)?(?:domaintoavoid1|domaintoavoid)\.com$ [NC]
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Make sure to clean your browser cache before testing this.

mod rewrite Force HTTPS + WWW

I'm currently using the following to force www. however I want to also force https
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How would I edit that correctly to force https AS WELL as www.
Fixed main site with:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Other domains on are now forced to include https I only want my main site to do it...
A different fix for main site:
RewriteEngine on
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^DOMAIN\.com$ [NC]
RewriteRule ^.*$ https://www.DOMAIN.com%{REQUEST_URI} [R,L]
Seems to work correctly. I moved my other domain to a different server
Here is a rule that you can use to force www and https in same rule:
# https and www in one rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
Ordering of RewriteCond is very important here otherwise %1 will not be captured correctly. First 2 conditions are or ORed together and 3rd condition is ANDed.

Htaccess rewrite rule and condition

We are now using OpenCart and domain aliasses in DirectAdmin.
We now have some rules in our htaccess for redirecting non www to www and non https to https.
But this rule is for all domains. We only want to use it for example-1.com
How can we do this? Htaccess we have tried all failled to do this.
This is the current htaccess we have
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https\:\/\/www.example-1\.com\/$1 [L,R=301]
We only want this for example-1 and example 2 also. But now it always redirects to example-1
I understand you only try to redirect example-1 and example-2 and not other domains.
You can use that:
RewriteCond %{HTTP_HOST} (?:^|\.)(example-1\.com|example-2\.com)$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
Try:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]

Rewrite subdomain to www counterpart

I want to redirect http://sub.domain.com to http://www.sub.domain.com but also have http://domain.com redirect to http://www.domain.com.
This is what I have for the domain part
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .? http://www.domain.com%{REQUEST_URI} [R=301,L]
How hard is this to do?
Here is the solution:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources