Apache/htaccess redirect all to https://www - apache

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.

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]

Htaccess on Forwarding http to https plus forcing www

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.

.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]

Mod rewrite does not work well in Mozilla

I am rewriting all http:// and http://www on my website to https://www
Everything works well on all browser except on Mozilla Firefox. When i try to access my website using http://example.com, it redirects to: https://www.example.com/https:/example.com/
My .htaccess:
RewriteCond %{HTTP_HOST} ^(example\.com)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule ^([^/]*)$ hosted_form.php/?username=%1&hash=$1 [R,L]
Please how can I fix this?
First of all your top 2 rules can be combined into one:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com$%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^([^/]+)/?$ hosted_form.php/?username=%1&hash=$1 [R,L,QSA]
Once that is done you need to clear your browser cache on Firefox since rewrite should behave same on all the browsers.

HtAccess: Force WWW but keep subdomain mask

I'm trying to force .www so Google doesn't pick up my website twice but when I do my subdomains no longer mask using [P].
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^((www\.)?)domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/website/%1$1 [L,NC,P]
When I visit a masked domain it simply just redirects and no longer masks. If I take out the top part the subdomains work fine but it then doesn't force .www
Try changing the order of rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/website/%1/$1 [L,P]
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]