htaccess rewrite rule fails on https redirection - apache

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.

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 allow one url to be accessed either via HTTPS or HTTP

I have a rewrite rule in .htaccess that redirects our site to HTTPS
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I need to leave one url open to work both HTTP and HTTPS (http://www.myhost.com/api and https://www.myhost.com/api). How do I need to modify my above code
Try below rule,
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/support/registration_service
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Above rule excluding the uri /api to be forced to https so you can use www.myhost.com/api for both scheme.

Redirecting to https for any URI

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]

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