khargoosh khargoosh - 26 days ago 17
Apache Configuration Question

mod_rewrite interaction between two .htaccess files

.htaccess
file located in web root
/


RewriteEngine on

RewriteCond %{HTTP_HOST} ^product.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.product.com$ [OR]
RewriteCond %{HTTP_HOST} ^product.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.product.com.au$
RewriteRule ^(.*)$ https://www.company.com/product/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^company.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.company.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^companyalternate.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www.companyalternate.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^company.com$
RewriteRule ^(.*)$ https://www.company.com/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


.htaccess
file located in
/product/blog/


RewriteEngine On
RewriteBase /product/blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?article=$1


As far as I can tell, URL rewriting is working as expected, until I enter something like:

company.com.au/product/blog/test-article


In this case, the resulting url appears exactly as entered, however the expected content is retrieved from the
/product/blog/index.php
script. So the
/product/blog/.htaccess
is being processed, but it seems the
/.htaccess
is not?

I would expect the above URL to be redirected to:

https://www.company.com/product/blog/index.php?article=test-article


And to appear as:

https://www.company.com/product/blog/test-article


Where am I going wrong?

Answer

As already been mentioned in comments/answer that Apache .htaccess is per-directory directive. Apache only uses closest/nearest .htaccess so in your case it is only using /product/blog/.htaccess and ignoring /.htaccess:

You can use this directive at top of your /product/blog/.htaccess:

RewriteOptions InheritBefore

This will make rules from the parent scope are applied before rules specified in the child scope.

Here is Apache documentation

Comments