HtAccess: Force WWW but keep subdomain mask - apache

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]

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.

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 redirect subdomain before forcing HTTPS?

I have the following rules on my website:
Force HTTPS on all pages.
Redirect http://user.domain.com to https://www.domain.com/file.php?user=user
The problem I'm having now is that when someone goes to http://user.domain.com , they get redirected to https://user.domain.com , which is then insecure, instead of to https://www.domain.com/file.php?user=user .
How do I fix this?
Here is the .htaccess content:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/file.php?user_id=%1 [L]
I also tried to change %{HTTP_HOST} to %{HTTPS_HOST} and it ended up with the same problem.
Try with:
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/file.php?user_id=%1 [L]
You can use these rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /file.php?user_id=%1 [L,QSA]

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.