.htaccess redirect everything except - .htaccess

I need to redirect all HTTP requests to HTTPS, except requests that are accessing my admin pages.
This should go to HTTP
http://example.com/admin/**/*
This should go to HTTPS:
http://example.com/blas.dfds
This is what I have, it will redirect everything to HTTPS. How to add filter to request that with "admin" keyword?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Have it this way:
RewriteEngine On
# http-https for everything except /admin
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/admin [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# https-http for /admin
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /admin [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Test this new set of rules in a new browser.

Related

Force HTTPS for some pages and folders and HTTP for all others

I have in my .htaccess and I want to redirect example.com/login, example.com/profile and example.com/form.php
it works only for folder but doesn't work for pages.
Here is my .htaccess
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php
RewriteEngine On
# Force HTTP to HTTPS
RewriteCond %{HTTPS} =off [NC]
RewriteCond %{THE_REQUEST} /(login|profile|form\.php) [NC]
RewriteRule ^(login|profile|form\.php) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
# Force HTTPS to HTTP
RewriteCond %{HTTPS} =on [NC]
RewriteCond %{THE_REQUEST} !/(login|profile|form\.php) [NC]
RewriteRule !^(login|profile|form\.php) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
So, if my user get in http://example.com/login, he is redirected to https://example.com/login. The same for profile page.
But, for single pages like example.com/form.php, it does not work
Please help me to write it in a correct way.
You may use these rules:
Options +FollowSymLinks -MultiViews
DirectoryIndex index.php
RewriteEngine On
# Force HTTP to HTTPS
RewriteCond %{HTTPS} =off [NC]
RewriteCond %{THE_REQUEST} \s/+(login|profile|form\.php) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# Force HTTPS to HTTP
RewriteCond %{HTTPS} =on [NC]
RewriteCond %{THE_REQUEST} !\s/+(login|profile|form\.php) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
# more rules go below this line
Make sure to test this in an browser or clear browser cache completely.

How to force http on specific folder in https site

I have site with https
But I need specific folder should be http.
How can we achieve it ?
e.g.
my site is
https://www.mywebsite.com
I want folder1/subfolder1 should be forced to http://
e.g. http://www.mywebsite.com/folder1/subfolder1
I searched on net...but maximum search show how to force http to https
I tried .htaccess as follows :
try 1.
RewriteRule ^(/folder1/subfolder1)($|/) - [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
try 2.
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/folder1/subfolder1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But it didn't work..
You can use these rules in your site root .htaccess:
RewriteEngine On
# force https on everything except /folder1/subfolder1
RewriteCond %{THE_REQUEST} !\s/+folder1/subfolder1[/?\s] [NC]
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# force http on /folder1/subfolder1
RewriteCond %{THE_REQUEST} \s/+folder1/subfolder1[/?\s] [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache or use a new browser for testing.

.htaccess: redirect from http to https except on some pages?

I would like to redirect all my website from http tp https except on some pages:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ex: pages to be excluded :
/home /user /info /mydata /ajax.php
Add yet another RewriteCond directive:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/(home|user|info|myta|ajax\.php)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
or in a single rule:
RewriteCond %{HTTPS} off
RewriteRule ^/?(?!home|user|info|myta|ajax\.php) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess exclude specific folder

for a website I need a redirect from
domain without www TO domain with www
and
http TO https
The final URL has to be always https://www.myshop.com ...
I solved this successfully with this commands:
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
The problem ist that I have to exclude a specific folder.
The URL to exclude is: http://www.mydomain.de/myfolder/filename.php
This means: If this URL is requestet there mustn't be a redirect.
Other tutorials I found did not work, sorry :-(
Thanks for any help and best regards...
You can have a skip redirect rule before these 2 redirect rules you have:
RewriteEngine On
# skip myfolder/filename.php from any redirects below
RewriteCond %{THE_REQUEST} /myfolder/filename\.php[?/\s] [NC]
RewriteRule ^ - [L]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Maybe something like that
RewriteEngine on
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteRule ^http://www.mydomain.de/myfolder/filename.php$ - [L]
RewriteRule ^.*\filename.php$ http://www.mydomain.de/myfolder/filename.php [R=301,L]
Adding something like this as a first ruleset may help. (May want to revisit those [L,R] options though.
# check if myfolder is contained
RewriteCond %{REQUEST_URI} ^[^/]*/myfolder(/.*)?$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if http, redirect to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# if without www, redirect to www (301)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

How to redirect a specific URL with .htaccess

I want to redirect http to https some specific url like login, signup
currently i am using bellow .htaccess
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
using this all page redirect to https but i want to redirect only example.com/login and example .com/signup
RewriteCond %{HTTPS} =off
RewriteRule ^login https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} =off
RewriteRule ^signup https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources