I'm trying to redirect three cases to https://www. They are:
http://
http://www
https://
I've been able to get the first two, but not the last one. Here's what I'm using for that:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301]
You can use this rule to add www and turn on https in same rule:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
To capture value from %{HTTP_HOST} you need a condition that is not it OR condition.
Related
The code below redirects none-www to www and subsequently forwards to https://
how can I merge these in one so that I won't have a double redirect.
# Redirect non www to www
RewriteCond %{HTTP_HOST} ^mysite.nl$ [NC]
RewriteRule (.*) http://www.mysite.nl/$1 [R=301,L]
# Redirect non https to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can use OR condition to merge both rules :
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^mysite.nl$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
'ornext|OR' (or next condition) Use this to combine rule conditions with a local OR instead of the implicit AND.
Reference : http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
Try with below rule,
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^mysite.nl$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
in .htacces I am trying to redirect to https://www
this is working fine
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but with 2 redirections I'd like to do it with only 1 redirection
I have tried
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
it works fine but if the s is removed (step 2) I get 2 times www
1) https://www.domain.com --> 2) http://www.domain.com --> 3) https://www.www.domain.com
how can I redirect ?
http://domain.com
http://www.domain.com
https://domain.com
to
https://www.domain.com
with only one redirection and no error whatever happen
thanks for helping
This does only one redirection. Yes, it is still two rules, but only one redirection will be performed:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301,QSA]
A more compact alternative, though not really faster or more efficient, would be that:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301,QSA]
The third condition in this always matches, it is only used to extract the part of the host name without a potantial leading 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.
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]
I need to know how to redirect example.com/folder/ to www.example.com/folder/ using htaccess. At moment htaccess redirects example.com/folder/ to www.example.com. This is what htaccess says at moment:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder$ /folder/ [L,R]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder/(.*) /$1 [L]
What changes are needed?
Just use this condition:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
First check that the host value is not empty (may be with HTTP/1.0).
The check if we already "use" www and then chrck for SSL (https)
If you want to keep subdomains intact, you should change the second line to this (heads up this is not 100% generic since you have to hardcode in your domain!):
RewriteCond %{HTTP_HOST} ^example.com [NC]