HTTPS htaccess redirection with multiple htaccess file problem


#1

Hi, I can’t solve an issue so I’m asking for help here.

Let’s say I have example.com/.htaccess with the forced HTTPS redirection for any request.

Then in example.com/service1/.htaccess I have some rewriterule rules regarding that specific folder.

Problem is that HTTPS redirection on that folder doesn’t work anymore and I can access it both from HTTP and from HTTPS.

If I remove the rules (which I need) the HTTPS redirection works again.

I’ve searched the new a long time for a solution but didn’t find one. Anyone knows what to do?

Thanks in advance!


#2

it’s hard to tell without seeing what each individual .htaccess specifies. Apache applies the .htaccess rules in the order they’re found. So a directive in example.com/service1/.htaccess might negate one that is in example.com/.htaccess

You may want to read Apache HTTP Server Tutorial: .htaccess files and especially How directives are applied. If you’re still confused, share the content of your .htaccess.


#3

Thanks for the answer. I’ve created a test case to try solutions without affecting production site.

So I have example.com/.htaccess with this code:

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

Then I have example.com/test/.htaccess with:

RewriteEngine  on
RewriteRule page([0-9]+)\.php /test/page.php?id=$1 [L]

If I access http://example.com/ I’m redirected to https://example.com/ BUT if I access http://example.com/test/ or http://example.com/test/page42.php, I get correct result but without HTTPS redirection.

I then discovered that if I add the HTTPS redirection rules from main folder .htaccess also to the subfolder one, redirection works again. But is this the correct way to do it? And exactly why the subfolder htaccess overrides HTTPS redirection? Isn’t it executed after the first one?


#4

The Apache tutorial says that Rewrite Rules in .htaccess files make things different… You may want to explicitly make /test/.htaccess inherit from previous directory using RewriteOptions Directive. Check http://httpd.apache.org/docs/current/mod/mod_rewrite.html for details.


#5

Have you tried just making the redirect absolute?

RewriteRule page([0-9]+)\.php https://example.com/test/page.php?id=$1 [L]

#6

Thanks, interesting option! Sadly as we still don’t have Apache 2.4 on DH, it’s useless as inherited rules are executed AFTER local ones. Will be handy when DH finally update Apache and we will get options InheritBefore and InheritDown.

So for now I’ve just to copy the https redirection rules. Good to know this is the way. Thanks again!


#7

It doesn’t solve problem for other pages (including index) in example.com/test/


#8

I think you can force the inherit by putting this at top of the htaccess in the sub directory:

RewriteRule inherit

#9

The description for the option is really confusing, then, because… I tried and it works! Thank you all.


#10

You’re welcome… glad it’s working.


#11

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.