Does .htaccess inherit settings from parent directories?
In Litespeed and Apache, .htaccess files in your folder will also inherit settings from the parent directories unless overridden.
Example Domain: app.example.com
Document Root: /home/username/example.com/app
The following .htaccess files would be checked:
/home/username/example.com/app/.htaccess/home/username/example.com/.htaccess/home/username/.htaccess
A real-world example of this would be: You have all your addon domains located at /home/username/exampledomain.com, /home/username/exampledomain2.com, etc.
You insert a .htaccess file in /home/username/.htaccess to redirect all traffic to HTTPS traffic which might look as folows:
RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]Now for both exampledomain.com and exampledomain2.com, all traffic will be redirected to HTTPS even when it’s not explicitly set in their respective .htaccess files.
You can also cause problems such as specific redirects like /old-page to /new-page in the parent directory that you don’t want to apply to all domains:
Redirect 301 /old-page /new-pageThis would redirect /old-page to /new-page for all domains, even if you don’t want that redirect to apply to all domains.
Another common case is forcing www. on all domains but in one application you defined the opposite causing a redirect loop. The parent .htaccess file may have this:
RewriteEngine OnRewriteCond %{HTTP_HOST} !^www\.RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]And the application might have this:
RewriteEngine OnRewriteCond %{HTTP_HOST} ^www\.RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]This would cause a redirect loop.
We recommend avoiding using .htaccess files in your home directory when possible to avoid these issues and many others. We instead suggest being explicit about what you’re doing in your .htaccess files, and avoid using blanket rules for many domains.