How to enable SSL excluding specific directories AND excluding subdomains - .htaccess

I want:
Normalize "domain.com" to "www.domain.com"
Enable SSL, but:
Exclude SSL from /PHP/*
Exclude SSL from specific page "noSSL.htm"
This is what I have:
#enable SSL excluding /PHP directory
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule !^php/.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#Enable SSL and add "www." if missing
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ https%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Questions/Issues:
Issue: Subdomains gets SSL
How to exclude a specific file from SSL?
Do I need to repeat "RewriteEngine On"?
Any help would be very much appreciated!

As per your comment , The rules should look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule !^(php/.*|.*noSSL\.htm)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
So noSSL.htm will be excluded .
Note: clear browser cache then test.

Related

Forcing HTTPS & Non-WWW on all folders with exceptions in place

I'm running into an issue where by my subdomains are incorrectly getting www appended to them via a htaccess rewrite rule...
My folder structure is as follows:
/public_html/index.html (A maintenance page just in case)
/public_html/.htaccess
/public_html/websitename
/public_html/subdomain
/public_html/testsite
/public_html/clone
My /public_html/ that's located in my .htaccess looks as follows
# Force HTTPS & WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} websitename.com$ [NC]
RewriteRule ^(.*)$ /websitename/$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain.website.com$ [NC]
RewriteRule ^(.*)$ /subdomain/$1 [L]
</IfModule>
Which works perfectly for websitename.com, which forces the URL to be rewrote to https://www.websitename.com
However it makes my subdomains get incorrectly rewrote to https://www.subdomain.websitename.com when it should be https://subdomain.websitename.com
I don't want to have to put the forcing of HTTPS & WWW in the individual website folders... rather, I'm looking for a solution to make subdomain exempt from the rewrite rule.
I tried adding the following condition but it didn't help:
RewriteCond %{HTTP_HOST} subdomain.websitename.com$ [NC]
Anyone have any idea what I can do to get around this issue?
Remove:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
And add:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
For multiple domains you can use:
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteRule ^ https://www.%1.%2%{REQUEST_URI} [R=301,L]

.htaccess changes to move site to SSL/https

Those are the lines of concern currently in my .htaccess file,
note that i have two domains pointing to the same site:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R]
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
Now I need to convert my site to SSL, I was instructed to use the following
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think that this code misses my other domain and the www rewrite part,
how to enable SSL while accounting for my other domain and also for the WWW part?
You can use:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]

is my .htaccess correct for non-www to www AND to force https?

We are switching to HTTPS.
Our current .htaccess file has the following code to force non-www to www:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now, for HTTPS this is my proposed addition:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
My question is - how do I merge - or combine - the non-www to www AND HTTPS? Or would I just keep the order as is above?
Thanks.
PS I have researched this but I don't seem to find any concise answer....
You can use that:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,R=301,L]
Or without domain name:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
I recommend you use the first. If indicate the domain name is not a problem.

How to redirect example.com/folder/ to www.example.com/folder/?

I need to know how to redirect example.com/folder/ to www.example.com/folder/ using htaccess. At moment htaccess redirects example.com/folder/ to www.example.com. This is what htaccess says at moment:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder$ /folder/ [L,R]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder/(.*) /$1 [L]
What changes are needed?
Just use this condition:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
First check that the host value is not empty (may be with HTTP/1.0).
The check if we already "use" www and then chrck for SSL (https)
If you want to keep subdomains intact, you should change the second line to this (heads up this is not 100% generic since you have to hardcode in your domain!):
RewriteCond %{HTTP_HOST} ^example.com [NC]

htaccess - redirect all pages except checkout to HTTP non-SSL

I'm trying to redirect all traffic to HTTP unless it is our checkout process, in which case I'm trying to redirect it to HTTPS.
I have a Rackspace Cloud Sites setup however the below does not appear to be working. Is there something that I am doing incorrect in the below?
Thanks!
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
RewriteEngine On
RewriteBase /
#redirect to www
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#redirect all https traffic to http, unless it is pointed at checkout
RewriteCond %{HTTP:X-Forwarded-SSL} on
RewriteCond %{REQUEST_URI} !^/checkout/?.*$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#redirect all http traffic to https, if it is pointed at /checkout
RewriteCond %{HTTP:X-Forwarded-SSL} off
RewriteCond %{REQUEST_URI} ^/checkout/?.*$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
#Force NON-SSL on a non-checkout directories
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^/DIRECTORY/?.*$
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
#Force SSL on a specific directory
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^DIRECTORY/(.*)$ https://www.example.com/DIRECTORY/$1 [R,L]
Another way:
RewriteCond %{HTTPS} !=on
# change user|cart|admin to folders you want SSL on.
RewriteRule ^/?(user|cart|admin) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} !=off
# change content|category to folders you want SSL off.
RewriteRule ^/?(content|category) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Use this
#redirect all http traffic to https, if it is pointed at /checkout
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} checkout|login
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Resources