2

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?

1

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]
  • 2
    "redirect as a 403 Forbidden" - that bit doesn't quite make sense, you mean serve a 403 Forbidden. There is no "redirect" as such. When specifying the F, the RewriteRule substitution (ie. /non-existent) is ignored completely and Apache returns the 403 document. – MrWhite Sep 1 '17 at 11:56
0

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.

  • 3
    Apache will look all the way up to / if AllowOverride is set that way. See httpd.apache.org/docs/trunk/howto/htaccess.html – Dan Aug 23 '13 at 15:30
  • It sounds like they were expecting a .htaccess file outside the document root to take effect. – Stephen Ostermiller Aug 23 '13 at 15:48
  • 4
    .htaccess files can exist above the document root. The fact the environemnt var was set proves it was being called. – Dan Aug 23 '13 at 15:55
  • Good point on the environment var. – Stephen Ostermiller Aug 23 '13 at 16:19
0

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:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriteoptions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.