1

I have 4 sites hosted with the same ISP. The sites were modified endless times, and now, I have several .htaccess files. One of the is at the main root. The others are spread in different places. For example, they appear also on the root of every URL.

To explain it better, I'll call one site A and the second B. So now, I have the .htaccess files:

  • Root (with .htaccess)
    • A + its own .htaccess in public.html.
    • B + its own .htaccess in public.html.

I assume that these files have to be only on the root oh the public.html. Am I right?

  • It's not clear whether you have 4 independently hosted accounts with this ISP (web host?) or whether you have multiple "Addon domains" on a single account and the common .htaccess file in the root is common for all sites? – MrWhite Dec 26 '18 at 8:20
0

No, .htaccess files can be anywhere on a site.

.htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof. (my emphasis)

https://httpd.apache.org/docs/current/howto/htaccess.html

0

There isn't a single "active" .htaccess file - it's a little more complex than that. Without knowing how your server is configured or what is actually in these .htaccess files, we can only assume they are all "active". They all work together.

An .htaccess file is an Apache per-directory config file. It affects the directory where the .htaccess file is located and the entire directory tree below it. Server permitting, all .htaccess files along the filesystem path (that the URL maps to) are processed, starting with the parent directory and drilling down into subdirectories. So, in your example, the .htaccess file in the "Root" will be processed before the .htaccess file in A's public_html directory - if a file below A's public_html directory is requested.

However, different modules are processed independently across config files, which isn't necessarily the order in which the directives appear within any single .htaccess file. And how the directives are "inherited" between parent/child .htaccess files is also module dependent (mod_rewrite, in particular, has its own rules in this regard).

I assume that these files have to be only on the root oh the public.html. Am I right?

This would be an ideal; for easy maintainance. However, as mentioned above, they can occur anywhere. Depending on what you are doing, it may not even be desirable to have all directives in a common .htaccess file in the root. But neither should you arbitrarily disperse directives across the filesystem.

If you have access to the server config then you can more easily consolidate all .htaccess files into a single config (inside a <VirtualHost> container) - perhaps with multiple <Directory> containers therein.

0

I agree with Steve, what he has pointed out is correct. .htaccess file would impact the directory that it is placed in. Having said that if there is an additional .htaccess in the sub-directory the files in the sub-directory would inherit from the .htaccess file in the same directory and not in the parent directory.

As mentioned by Steve in his answer

.htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

So just to answer your question taking your example itself


- Root // .htaccess in root will impact all files and sub-directories 
  |
  |-- A
    |- .htaccess // Since A has its .htaccess all files and sub-directories in A will inherit from this .htaccess file
  |  
  |-- B  // If B does not have .htaccess it will inherit from the Root directory which has one.

Hope this helps !!

  • "the files in the sub-directory would inherit from the .htaccess file in the same directory and not in the parent directory." - this isn't strictly correct. Both .htaccess files can apply. In your example, files in A can be affected by both the .htaccess file in the root (parent directory) and the .htaccess file in the A subdirectory. It depends on the Apache modules being used, the specific directives and the server config (none of which is stated in the question). – MrWhite Jan 26 at 17:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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