Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent executing .htaccess of parent folder

I have two different sites on same domain for example:
First site in the root directory http://example.com/
Second site in subfolder http://example.com/site2/
Each site have his own .htaccess
When i enter to second site (http://example.com/site2/), in log of mod_rewrite i see that apache trying to execute .htaccess of first site (http://example.com/).
So, the question is how to prevent this? Thanks

like image 977
Alex Avatar asked Sep 03 '10 20:09

Alex


2 Answers

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions

inherit

This forces the current configuration to inherit the configuration of the parent. In per-virtual-server context, this means that the maps, conditions and rules of the main server are inherited. In per-directory context this means that conditions and rules of the parent directory's .htaccess configuration are inherited.

Rules inherited from the parent scope are applied after rules specified in the child scope.

like image 180
Mickey Mouse Avatar answered Oct 22 '22 08:10

Mickey Mouse


While you can not stop the execution of the .htaccess in http://example.com

You CAN place another .htaccess in http://example.com/site2/ and any rule created here that overrides the rules in the .htaccess of the parent folder will be executed here but not in the parent folder.

For example, I recently had a case where I was rewriting any non-www version of my main site, which we'll call example.com to its www equivalent: www.example.com

The problem this caused is anyone going to my second site, which we'll call site2.com would actually redirect the user to www.example.com/site2.com/ even if the user enters site2.com OR www.site2.com OR http://www.site2.com in their browser's address input

What I had to do to fix it is makes changes in 2 .htaccess files, first in the root http://www.example.com then in a separate .htaccess located in http://www.example.com/site2.com/

1) http://www.example.com .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

2) http://www.site2.com (http://www.example.com/site2.com/) .htaccess(this also forces the www version of site2.com):

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.site2\.com$ [NC]
RewriteRule ^(.*)$ http://www.site2.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^site2\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com/site2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.site2\.example\.com$
RewriteRule ^/?$ "http\:\/\/www\.site2\.com\/" [R=301,L]

Feel free to rip apart my regex, I am by no means a regex master. I also realize that this can creates 2 or maybe more extra redirects but this is the only way I could get this to work, without reassigning my DocumentRoot through through the host...which is probably a better option if your host can do it, mine would/could not...

like image 39
Serj Sagan Avatar answered Oct 22 '22 09:10

Serj Sagan