Htaccess on Forwarding http to https plus forcing www - apache

I use the following code in my .htaccess
RewriteCond %{HTTPS} off
RewriteRule !^(index(\.php)?)?$ https://%{HTTP_HOST}/$1 [R=301,L]
this work well for https when i add
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} off
RewriteRule !^(index(\.php)?)?$ https://www.%{HTTP_HOST}/$1 [R=301,L]
to force www infront it works only to force www infront but does not forward http to https.
any help on this

To enforce www and htpp->https in same rule use an OR between 2 conditions:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{THE_REQUEST} !/index\.php [NC]
RewriteRule . https://www.%1%{REQUEST_URI} [R=301,L,NE]
3rd RewriteCond is for capturing value of domain after www.

Related

How to disable https redirect on a single page with htaccess

I am trying to disable https on a page on my site which also have a dynamic sub pages. For example http://example.com/page/dynamic/section.
I have try below htacess code
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/page/* [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But still get stuck and it redirecting to https please any one?
I am trying to disable https on a page on my site which also have a dynamic sub pages. For example http://example.com/page/dynamic/section:
You may use this .htaccess code:
RewriteEngine On
# add www if missing to all the URIs
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# turn on https except for a specific URI
RewriteCond %{HTTPS} !on
RewriteCond %{THE_REQUEST} !/page[/\s?] [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# force http for a specific page:
RewriteCond %{HTTPS} on
RewriteRule ^page(/|$) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,NC]

force all user to non-www and https

I want to redirect all my visitors to https://example.com
I have found two codes to implement this, Please tell me which one should I use?
First one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Second One:
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

Apache/htaccess redirect all to https://www

I got the following .htaccess
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTPS_HOST} ^www\. [NC]
RewriteCond %{HTTPS_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What I want to do is redirect everything to https://www.
I thought that my htaccess should do exactly that, what's my mistake?
Your rewrite conditions are contradictory and will stop execution of rule. You can use this single rule to do both https and add www:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
This will create a 302 (temporary) redirect. You can also use
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
to create a 301 (permanent) redirect.

.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 force https & www. on domain & http no www on subdomain

How do I get in htaccess to force https & www. on domain, & http no www on subdomain?
The following redirects the http://sub.domain.com to https://domain.com
# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://sub.domain.com%{REQUEST_URI} [R=301,L,NE]
You can use:
# for main domains
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]
# for sub domain
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ http://sub.domain.com%{REQUEST_URI} [R=301,L,NE]
You need to test this after clearing your browser cache.