how to redirect everything to SSL https except a few urls - apache

In my htaccess i use the following rule to redirect everything under SSL,
now i need to add a few exception, for example
http://www.mysite/blog-feed.html must be reachable for the http protocol.
#ALWAYS REDIRECT TO SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Can just try:
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/blog-feed.html
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

How do I do https for a whole website, except for one folder?

I have this htaccess file, which rewrites everything to https:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Is it possible to make an exception for a single folder, so it runs on normal http?
The following RewriteCond can be used:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Uses http for:
http://example.com/test/abc.txt
but redirects to https for:
http://example.com/hello/abc.txt

Https to http redirect using htaccess for a single PHP file

I am trying to redirect https://demo.aurazoscript.com/surf.php?id=15 to http://demo.aurazoscript.com/surf.php?id=15. I tried the following in .htaccess but that doesn't work. It turns off forced HTTPS redirection for the whole website which I don't want. I only want it to be turned off for that single page surf.php.
I tried in .htaccess
# FORCE HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} \/(surf.php)
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# DISABLE HTTPS
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !\/(surf.php) [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
I am using Flexible SSL from CloudFlare.
I think you do it opposite way , try this at the beginning of .htaccess file after RewriteEngine On:
RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
RewriteCond %{HTTPS} on
RewriteRule ^surf\.php http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTPS} off
RewriteRule !^surf\.php https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note: clear browser cache then test .

host exception in .htaccess for http to https redirection

I have defined the following code in my .htaccess file, how do I make an exception for http://mydomian.com to not be redirected to https ?
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
(Please note that somewhere on my site I'm loading http://mydomian.com via iFrame and on that website it doesn't have the https working properly).
To exclude www.mydomain.com you can use the following RewriteCondition :
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
Try this :
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Force HTTPS causing 404 redirect

I'm using .htaccess to force redirection to https://www.domain.com using the following code:
RewriteEngine On
RewriteCond %{HTTPS} !off
# First rewrite to HTTPS:
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The above result in a page with encrypted output:
Is this a problem on my htaccess or, a server problem?
CloudFlare doesn't use 443 port so, try this:
RewriteCond %{SERVER_PORT} 80
A server problem would be noted with a 5xx error. Check your logs and try this:
RewriteCond %{HTTPS} !on [OR]
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "https://www.%{HTTP_HOST}/$1" [L,R,NE]

Switching between http and https page on single website

We are running a web-site with both HTTP and HTTPs protocols. We need a rule for .htaccess file where we can handle these requests i.e. non-https page should redirect with http:// and https page should redirect with https:// protocol.
We are using apache. Any help would greatly appreciate.
Add this to your .htaccess file at your site's root directory.
RewriteEngine on
# Force http://
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} ^(/|/dl/memorandum.*)$ [NC]
RewriteRule ^ http://%{HTTP_HOST}/ [R=301,L]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/contact$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can match on multiple pages by using the | OR operator.
^/(contact|profile|transactions)$ [NC]
If the URLs are long you can drop the $ and do a prefix match as
^/(contact|profile|transactions) [NC]
This will match on /contact-page or /contact/page as well now.
Use negation to force HTTP on all the rules that do not require SSL.
RewriteEngine on
# Force http://
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(contact|login|cart|register|forgot-your-password|request)$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} ^/(contact|login|cart|register|forgot-your-password|request)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]