I have a webserver with thousands of directories. Some need to be internal only, some need to reference specific IP addresses, some are public. These .htaccess files change from time to time with IP addresses and blocks being added and removed. Today, each directory (and many subdirectories) have their own .htaccess file based on the traffic that's allowed to access it (some subdirectories don't have an .htaccess file, they inherit the parent directory's .htaccess rules).

Is there a way to organize my directories and .htaccess files so that I only have a few main .htaccess files and each subdirectory can reference an .htaccess based on what access they need?

For example:

public .htaccess
restricted .htaccess
private .htaccess

    use "public .htaccess"
    use "public .htaccess"
      use "private .htaccess"
    use "restricted .htaccess"

You can't do something quite like this at the Apache web server level. However, you could potentially create symlinks from .htaccess in the subdirectory to the appropriate .htaccess file stored elsewhere, so you'd only have one file to update.

If you have access to the server config then you could centralize this and have each "config file" as a separate include for the respective <Directory> container(s).

However, whether it actually makes sense to do this or not will depend on the specific directives you are using. For instance, mod_rewrite directives are processed relative to the directory where they are contained - so these would need to be written to be independent of the directory-prefix.

Aside: Having too many .htaccess files strewn across the filesystem can become difficult to maintain. It may be preferable (depending on the directives) to combine these into a single .htaccess file in the root?

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.