Currently, I have following redirection setup in htaccess to redirect www and non-www URL to HTTPS with www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have now created a subdomain dev.example.com. But when I run it, site redirects to HTTPS with www. How can I stop it, and load dev without any redirection?
Add another condition to the HTTPS rule:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !dev\.example\.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
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]
My domain www.abc.com is now redirecting to https://www.abc.co.uk
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]
These code works exactly what I want. After that I created a subdomain. www.x.abc.co.uk
Now I want to redirect that to http://x.abc.co.uk (without https)
I used this
RewriteCond %{HTTP_HOST} ^x\.abc\.co\.uk [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But browser says it has redirect loop.
How can do that?
If these are two different htaccess files then give this a try.
RewriteEngine On
#redirect www.x.abc.co.uk without www to http
RewriteCond %{HTTP_HOST} ^www\.x\.abc\.co\.uk [NC]
RewriteRule ^(.*)$ http://x.abc.co.uk%{REQUEST_URI} [L,R=301]
I modified your original rule so you don't have two rewrite rules and still direct to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*)$ https://www.abc.co.uk%{REQUEST_URI} [L,R=301]
I want to redirect from any direction to our site with HTTPS protocol, but some redirects it's not working. I want this:
http://www.site.co TO https://www.site.co
http://site.co TO https://www.site.co
This is my htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
The second rule it's not working. It going to another direction inside our site, and it isn't redirect to HTTPS site.
Try it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The only real difference here is that first we redirect from non-WWW to WWW then we check for HTTPS and redirect it.
If it does not work, try this one:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
The answer by Prix works. To make it more dynamic lets use SERVER_NAME and REQUEST_URI instead of a static domain name.
RewriteEngine On
#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
#here we dont use www as non www was already redirected to www.
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Sorry I don't have enough point to comment, but the rule of #prix will bring unneeded redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
You can try http://domain.com/ on GTMETRIX and you will get this message
"Avoid landing page redirects for the following chain of redirected URLs."
http://domain.com/
http://www.domain.com/
https://www.domain.com/
To prevent that, go to the first RewriteRule and add a "s" at the end of http. The new set of rule will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
This is http to https and non www to www redirection using .htaccess
If you want to redirect to https and www you can use this code
http to https redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
Non www to www redirection:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
If you want to use booth functionality place the above all code respectively
In case you are using One.com as your webhost you should use the following code instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have an asp.net website and the host provider is not supporting IIS rewrite module i have tested all this methods and only this code gives woorank.com confirmation maybe useful for another .net developer :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [L,R=301]
This will use for both www or non-www
If you try to open link with www then url redirect to https with www
Example : http://domain.com redirect to https://domain.com
or If you try to open link with non-www then url redirect to https with non-www
Example : http://www.domain.com redirect to https://www.domain.com
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Just put the following in the .htaccess file
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [END,NE,R=permanent]
Try this methode:
# Redirect 301 all to https://www
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
</IfModule>
Why sometimes you don't need code %{HTTP_HOST} and use your domain exact url with www for the last line, because usually the result will have two www like:
https://www.www.yourdomain.com
Btw if you use cloudflare, just create pagerules redirect 301 all to https and www version.
Create pagerules
Fill with
example.com/*
Choose setting forward url then choose 301 and fill with this
https://www.example.com/$1
Save it and all will be redirected to https and www version.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
For example https://example.com should be going to https://www.example.com
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L]
Note - you should change example.com to your own domain.
I'm trying to force a user to be redirected to the non-www website, and, force https.
I've got this which sort of work, but doesn't force https, when http is entered.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://site.com\.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Any ideas on what I'm doing wrong?
Try this rule:
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]
Based on Gumbo's comment : "the TLS/SSL connection is established and certificate is validated before it is handed down to HTTP and the HTTP redirection takes place"
I gave this a try (which seems to work):
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.blahblah.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www\.blahblah\.com [NC]
RewriteRule ^(.*)$ https://blahblah.com/$1 [L,R=301]
please tell me if there is something wrong with this approach.
The only set of rules that works for me is the following
# match any URL with www and rewrite it to https without the www
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [R=301,L]
# match non https and redirect to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
The order matters, it prevents a third redirect in some cases.
With this code I rediret from http and www and none www to https none www. Just pay attenstion that the place you insert the code in htaccess is important:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]