Forum Topic: Do htaccess files overwrite general settings in folders?

Forum: .htaccess Forum : Basics • Posted by Leon Fernandez • Updated:

In order to explain myself what I mean is that if an htaccess file in a subdirectory renews the directives and forgets about the previously defined ones in upper levels of the directory tree?

For example, if I have a VPS and in the vhosts directory I declare a generalized htaccess file with overall directives that serves for all domains (like charset, language, timezone, etcetera), when declaring specific htaccess files in htdocs directory for each domain, does it consider the previously declared directives in the upper vhosts directory? Or is it a better practice just to declare directives individually for each domain even if they are repetive?

Illustration:

vhosts/.htaccess
set charset
set language
set timezone
set permissions
etc.

vhosts/domain1
rewrite
redirect
etc.

vhosts/domain2
rewrite

vhosts/domain3
restrict, etc.

3 Replies to “Do htaccess files overwrite general settings in …”

Posted by Jeff Starr

.htaccess works in cascading fashion, much like CSS. This means that .htaccess directives specified in a directory will in most cases overrule any directives specified in any parent directories. As for best practice, it really depends on your own strategy, the rules involved, and the setup with which you are working. Whenever possible, for example, I like to avoid redundancy and keep all directives in the same file.

Posted by Leon Fernandez •

Thanx for the fast reply, so if I understand well, what you do is specify only one htaccess file for all domains in parent directory?

Also, when you mention overruling, do you mean that if I set a general htaccess file in the parent vhost directory (in the case previously mentioned), with a directive for Defaultcharset for example, and for domain1 create another htaccess file it will forget about the defaultcharset even if I didnt mention it in the domain1/htaccess file?

Posted by Jeff Starr

“Thanx for the fast reply, so if I understand well, what you do is specify only one htaccess file for all domains in parent directory?”

It depends on the server setup, really. I guess the general rule would be to use as few .htaccess files as possible. So for most of my projects, that means using just one .htaccess per domain. In some cases when working on a project with multiple sites/domains, if there are some .htaccess rules that need applied to all sites, I will use the main configuration file in order to apply a single set of rules that will be applied to all of them.

“Also, when you mention overruling, do you mean that if I set a general htaccess file in the parent vhost directory (in the case previously mentioned), with a directive for Defaultcharset for example, and for domain1 create another htaccess file it will forget about the defaultcharset even if I didnt mention it in the domain1/htaccess file?”

This is where it gets tricky, as rules vary in terms of scope; some rules apply to all domains and others may be overridden. My best advice is to investigate each directive that you are considering and then applying it in the most comprehensive way possible. This will eliminate redundancy and keep things as simple as possible going forward (which is good for maintenance, etc.).