Redirecting to https for any URI - apache

In my .htaccess file, I have the below.
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]
This works fine until someone goes to http://www.example.com/test because that does not force a redirect to https.
Is there a better way of doing this?

Yes, change your last two lines to:
RewriteCond %{HTTP_HOST} !^(www\.example\.com|)$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Related

rewrite/redirect http:// and http://www to https:// using htaccess

I'm new to editing htaccess files. What I want to do is:
rewrite/redirect specific subdomains eg subdomain1, subdomain2, etc from:
https://www.subdomain.example.com
http://www.subdomain.example.com
http://subdomain.example.com
To:
https://subdomain.example.com
I've pieced the below code together from other questions, but it doesn't seem to be working.
Can anyone explain what I'm doing wrong and help me fix the code.
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
To canonicalise ("redirect") only the specific subdomain ...
https://www.subdomain.example.com
http://www.subdomain.example.com
http://subdomain.example.com
To:
https://subdomain.example.com
You would need to do it something like this near the top of your .htaccess file:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(subdomain\.example\.com) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
The 3rd condition ensures that it only applies to the specific subdomain.
To make this apply to several subdomains (assuming the no-www is canonical for all subdomains) then you could modify just the 3rd condition to identify the subdomains that it should apply to. For example:
RewriteCond %{HTTP_HOST} ^(?:www\.)?((?:subdomain1|subdomain2|subdomain3)\.example\.com) [NC]
Test with a 302 (temporary) redirect to avoid potential caching issues. You will need to clear your browser cache before testing.
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This is along the right lines, but it fails to target HTTPS or the www sub-subdomain. Also, checking %{HTTPS} !on and %{SERVER_PORT} ^80$ are really the same thing.
You can use the following :
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R,L]
This will redirect all subdomains from http://www to https:// .
For the subdomain.example.com you can use the following :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^ https://subdomain.example.com%{REQUEST_URI} [R,L]

Can't redirect to https (htaccess)

I was trying to configure my htaccess file which will redirect from http to https with following lines of statement. But it couldn't .
Note: i don't want the www before the site URL as well.
What could be possible error of my code. Anyone please suggest.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,L]
Problem appears to be this line:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
This line makes this rule work for domain with starting www only.
You need to make starting www optional by using:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
As another solution, if you have only one domain name (www.)example.com, you can use more readable:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Also, you can also redirect based on port number, for example:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
This will redirect all requests received on port 80 to HTTPS.

.htaccess always redirect non-www to www except subdomains?

I searched before posting this but couldn't find a topic that fits my needs. My site is https only and I want to always redirect any request of https://domain.com to https://www.domain.com .. EXCEPT when somebody accesses https://username.domain.com then I need it redirected to https://www.domain.com/user?username=...
Right now I have it like this:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/user?username=%1 [L,QSA]
But it's not working with the non-www redirect to www. What part do I need to change and how?
I checked the other answer but it doesn't work as soon as I add the /user? rule...
You can have your rules as this:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com/user?username=%1 [L,QSA,R=302]

htaccess rewrite rule fails on https redirection

my htaccess look like this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^localhost$
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^login/?(.*)$ login.php$1 [L]
RewriteRule ^register/?(.*)$ register.php$1 [L]
i want to redirect my pages login and redirect to https and so i added the code below
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
so when i type http://www.xyz.com/login/ it redirects to https://www.xyz.com/login/ everything was fine when i decided to redirect the pages to http which are not login or register and i wrote
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)/?$
RewriteRule .*$ http://%{HTTP_HOST}%{REQUEST_URI} [L]
after that i testted the url http://www.xyz.com/login/ again and it redirects me to https://www.xyz.com/login.php
any idea to redirect http://www.xyz.com/login/ to https://www.xyz.com/login/ and on that page when i click the link https://www.xyz.com/news/1/ redirect to http://www.xyz.com/news/1/
Try adding this right below the RewriteEngine On line:
RewriteCond %{HTTPS} off
RewriteRule ^(login|register)(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^(login|register) http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
It also looks like you've pasted your rules wrong, the two blocks of rules are identical.

Force SSL on your website using .htaccess and Forcing the "www." at the beginning of URLs

I was using this code and it successfully forces ssl:
RewriteEngine On
RewriteCond %{HTTP_HOST} www\.example\.com
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
however it does not force www at the begining of urls. I got this supposed solution posted on another page here:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
But what i get when i attempt to open my page is this:"Firefox has detected that the server is redirecting the request for this address in a way that will never complete."
How may i fix this, that is, not only force ssl but also force www at the beginning of urls?
Thanks.
Try:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]