Mobile app version of vmapp.org
Login or Join
RJPawlick198

: One .htaccess file with rewrite rules for all domains Our server is configured with multiple domains which reside in subfolders of the public_html folder. So domain1.com is located in /home/user/public_html/domain1.com

@RJPawlick198

Posted in: #Htaccess #ModRewrite #UrlRewriting

Our server is configured with multiple domains which reside in subfolders of the public_html folder. So domain1.com is located in /home/user/public_html/domain1.com and domain2.com is located in /home/user/public_html/domain2.com and so on.

I've placed a .htaccess file in /home/user/public_html so I can put settings in it that apply to all domains. I've added two things to this .htaccess file. One is a special environment variable which is getting recognized by PHP just fine. The other is a redirect for any folder called .svn to a file called non-existant. So my .htaccess file looks like this:

RewriteEngine on
RewriteRule /.svn /non-existant
SetEnv ENVIRONMENT PRODUCTION


The problem is that the rewrite rule isn't working at all. The environment variable is, so I know this file is being processed by Apache, but the rewrite rule doesn't work. I've tried several other rewrite rules and none of them seem to be working.

Do rewrite rules only work if they are in the document_root?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @RJPawlick198

3 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

If you have a .htaccess files in a subdirectory (eg. /public_html/domain2.com/.htaccess) that contains mod_rewrite directives (even just RewriteEngine) then the mod_rewrite directives in your parent .htaccess file will be completely overridden and won't execute. This is the default behaviour. mod_rewrite directives are not inherited by default (in a directory/.htaccess context).

However, you can override this by explicitly stating RewriteOptions inherit (there are other options on Apache 2.4+) in the child .htaccess file. However, note that the parent mod_rewrite directives are literally copied in-place into the child .htaccess file (which naturally alters the directory-prefix - which might be a problem depending on the directive.).

On Apache 2.4+ you can inherit the other way ie. from parent to child, rather than child to parent (which is far more convenient in this case). So, in the parent .htaccess file (in /public_html), you can state:

RewriteOptions InheritDownBefore


Which will enable the parent mod_rewrite directives to be inherited by all child configs and it will execute before the child config.

Just to emphasise, this only relates to mod_rewrite. Other modules inherit by default. Each Apache module is processed independently.

Reference: httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriteoptions

10% popularity Vote Up Vote Down


 

@Rivera981

You're not matching the .svn folder correctly:

RewriteEngine on
RewriteRule ^(.*/)*.svn/ /non-existant


I would actually redirect as a 403 Forbidden:

RewriteRule ^(.*/)*.svn/ /non-existant [F,L]

10% popularity Vote Up Vote Down


 

@Eichhorn148

Apache only looks for .htaccess files within the document root. It sounds like you have multiple document roots -- one for each domain. The only place that you could put a central rewrite rule for all of them would be in Apache's httpd.conf file.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme