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

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]

Related

How to prevent subdomains from redirecting to https - htaccess?

I am trying to work out how to redirect all http instances to https:// though excluding the subdomain.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example.co [NC]
RewriteRule (.*) https://example.co/$1 [L,R=301,QSA]
For example, domain.example.co should be left as it is and not redirected to https://.
example.co or example.co/sub/sub should be redirected to https://
I have tried changing the RewriteRule:
RewriteRule ^$ https://example.co/$1 [R,L]
This leaves subdomains as they are but has no effect on subdirectories under example.co - i.e. example.co/sub/sub.
How could I redirect from http to https but exclude all subdomains?
Note
I also have a rewrite rule which points subdomains to their directories without changing the URL:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
sub.example.co will displays example.co/sub but the URL will not change
You can try this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(.+\.)?domain\.com$
RewriteRule ^(.*)$ https://%1domain.com/$1 [R,L]
Or This :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R,L]
Note: Replace sub with subdomain and domain with domain name
Enjoy it ;-)
Try below I am assuming you are not using www appended to host.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^example.co [NC]
RewriteRule ^(.*)$ https://example.co/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
ProxyPassReverse / http://example.co/

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

Redirect all website link www to non www and http to https using .htaccess

When I go to https://www.example.com/webinars.php.
I want to be redirected to https://example.com/webinars.php
But Its not working.
My current .htaccess file is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
Is this what you want?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

All http traffic to domain.com and all https to sub.domain.com with htaccess

I have two domain names www.domain.com and sub.domain.com, and I want to redirect all the http traffic to www.domain.com and all the https traffic to sub.domain.com. For example, if the url called is http:// sub.domain.com/index.php, the server have to rewrite the url to http:// www.domain.com/index.php, and if the url called is https:// www.domain.com/index.php, the server have to rewrite the url to https:// sub.domain.com.
I have this code in htaccess but it does not work correctly:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?$ "http\:\/\/sub\.domain\.com\/" [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
It redirects correctly the https to sub.domain.com but only when there is no page indicated (https:// domain.com/index.php/some-page not work and https:// domain.com yes). For the http not work at all.
Thanks in advance!
You can put this code in your root htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Make sure both domain.com and sub.domain.com share the same document root folder. Otherwise, you will have to split htaccess code in two htaccess (one per root domain folder)
/domain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
/subdomain/root/folder/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

Remove www and add https to a domain

I have http://www.example.com and I want it to be https://example.com.
I'm using the following .htaccess rule but it's not redirecting properly from https://www.example.com
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]