.htaccess allow one url to be accessed either via HTTPS or HTTP - apache

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.

Related

htaccess RewriteRule to https doesn't work combined with other rules

I'm trying to set a few rules in my .htaccess to work in a specific way:
Specific page redirections:
RewriteRule "^web/site/mais$" https://www.main.com/front/for-you/insurance/ [R=301,L]
Home redirection
RewriteRule ^$ https://www.forexample.com.br/for-you/ [R=301,L]
RewriteRule ^/$ https://www.forexample.com.br/for-you/ [R=301,L]
HTTP to HTTPS and add www to the rest (this is what doesn't work), i tried many things:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS} !^on$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS} !^on$ [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Other way I tried forgotting the www rule (but doesn't work too):
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The only way I found to combine is writting the general https and www rule in the httpd.conf and the other rules into the .htaccess, but this way I get two or more 301 redirects, bad business for SEO.
Do you have the same problema with all the directories or only with some?
Because if you have ProxyPass (as you said), maybe it send you the redirections directly to Tomcat, so maybe you need to specify the rules into the ssl.conf to read that rules before leave the ssl.conf. Try to write the last you wrote into the ssl.conf after the ProxyPass
If you want to redirect some spacific pages to https, forexample, To redirect :
http://example.com/page.html
to
https://www.example.com/page.html
And
http://example.com/page2.html
to
- https://www.example.com/page2.html
You may try the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
If you want to redirect the whole site from http to https, you can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]

Combining Rules in .htaccess file?

I'm wondering how to connect these two rewrite rules in one .htaccess file. Currently, I force https connections to my website with this rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteCond %{HTTP_HOST} !=SERVER_NAME
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Now I want to redirect from my www-subdomain to my real domain. There is a high rated answere here:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I fail to put these two together.
You can use:
RewriteEngine On
# if with www, redirect to https domain without www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
If you test first for www and redirect to https, no need to test for http://www.
Or like #PanamaJack said, you just can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,L,R=301]
But you need to change the domain name.

Redirect https to non http and Http to Https in directory with htaccess

I want to make my homepage to redirect to non https if it https
if https://example.com will redirect to http://example.com
and i want to make directory with ssl (just directory using SSL)
if http://example.com/directory wil redirect to https://example.com/directory
I'm use this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
but it just show loop
RewriteEngine On
RewriteCond %{HTTPS} ^on$
RewriteCond %{REQUEST_URI} !^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} ^off$
RewriteCond %{REQUEST_URI} ^/dir
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect https and http www to https non www

I want to rewrite the following
http://example.com
http://www.example.com
https://www.example.com
to
https://example.com
tried using
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
It doesn't seems to be working
I'd say the easiest thing to do is just write two rules:
# First make sure HTTPS is being used
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Then make sure www isn't being used
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can use:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]

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.