Advise on htaccess files, please

Continuing the topic of my large site split into 6 separate projects, I’d love to get your advise about the use of the htaccess files.

I’ve placed an htaccess file in the root of public_html folder. On the same level, I have folders that I exported those sub-projects into. Should I copy my main htaccess file and put copies into each folder with sub-projects? Or, is one main file in the root enough to serve the entire domain?

Any other thoughts on this subject?

htaccess files work on the current or subdirectory level. So any rule in the public_html directory will apply to any subdirectory of it.
So for most all rules like redirects than only the one is needed. If you start adding things for security like Htpassword files then placing them in other directories would be needed.

So, if I write nothing else but the specific rule in htaccess file designed for a specific subdirectory, all other general rules from the root still apply to that subdirectory? Or, do I need to write all general rules plus the specific rule in htaccess for that subdirectory?

Rules in the highest level directory apply to that directory and any subdirectories and any sub-sub-directories.

The htaccess file(aka local directives file) was originally intended to be used for security, hence the name htaccess (HyperText Access). The Apache web server will look for the file in each directory level to process a request. If it finds an htaccess file it reads it from disk and processes the rules in that file.

So if a request is for https://example.com/aboutus/termsofuse/

Then Apache will:

  1. look for an htaccess file and process directives in the root level directory (public_html in your example)
  2. Look for an htaccess file and process directives in the aboutus directory
  3. look for an htaccess file and process directives in the termsofuse directory

There is overhead with htaccess files so from a performance perspective the fewer files you have and the fewer rules in them the better off you are.

4 Likes

Thanks, Doug. That was a comprehensive reply…