htaccess https in parent but http in subdirectory

htaccess https in parent but http in subdirectory

I’ve tried implementing a number of suggested solutions to this problem but can’t get anything to work.
I am using 2 .htaccess files, one in my root directory which reads as follows:
RewriteEngine on
RewriteBase /
RewriteRule ^(webservice|api) – [L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [R=301,L]

This redirects all traffic to the main website through https which I need.
However in a subdirectory I want to disable the reroute and force all requests that go to this directory through http. The .htaccess file in /webservice/rest/ reads:
RewriteEngine On
RewriteBase /webservice/rest/
RewriteCond %{HTTPS} on
RewriteRule (.*) http://www.domain.com/webservice/rest/%{REQUEST_URI} [R=301,L]
RewriteRule ^testingPost/?$ testingPost.php [NC]
RewriteRule ^([a-zA-Z0-9\_\-]+)/?(\?([a-zA-Z0-9\_\-\=\?\&]))?$ otherfiledirect.php?method=$1&params=$2 [NC]

There are a few redirects taking place, but the 3rd and 4th line in the 2nd htaccess don’t seem to be doing anything – can anyone see what I’m doing wrong and how to keep https on the main site but force http on any url below /webservice/rest/ ??
Cheers

Solutions/Answers:

Solution 1:

The problem was with the ssl config file in the server. Allow Override needed to be set to All under the secure virtual host at port 443. After changing that, the following code in the root htaccess file allowed me to force https across the whole website except a specific folder:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/webservice     [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}  [R=301,L]

So any request to http would go to https. However I still needed to explicitly change requests to https on my specified folder to redirect to http, so added the following code after the above:

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} /webservice      [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Bullet proof.

Related:  Redirect 301 www.www.example.com

Solution 2:

Try this in /webservice/rest/.htaccess and make sure to test in a new browser:

RewriteEngine On
RewriteBase /webservice/rest/

RewriteCond %{HTTPS} on
RewriteRule ^ http://www.domain.com/%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^testingPost/?$ testingPost.php [L,NC]

RewriteRule ^([\w-]+)/?(\?([\w-=?&]))?$ otherfiledirect.php?method=$1&params=$2 [L,NC]

References