I have two domains:
maindomain.comwww.addondomain.com
A .htaccess file in maindomain.com has this redirect
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?maindomain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
It redirects www to non www version.
Another .htaccess file in addondomain.com have these redirect
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^addondomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.addondomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addondomain\.maindomain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.addondomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^maindomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.maindomain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.addondomain\.com\/$1" [R=301,L]
All pages redirect well, but when I put another .htaccess file in a subdirectory, the redirect does not work. So, I write these rules in image subdirectory.
RewriteCond %{HTTP_HOST} ^addondomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.addondomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addondomain\.maindomain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.addondomain\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^maindomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.maindomain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.addondomain\.com\/$1" [R=301,L]
Now when I try to access addondomain.maindomain.com/image/1.png it redirects to addondomain.maindomain.com/1.png with a 404 error. Some how, the image directory is lost. I need another .htaccess file in subdirectory.
UPDATE#1: addondomain is subdomain of maindomain also, directory structure is:
/public_html
.htaccess
home.php (page of main domain)
/addondomain.com (directory)
home.php (page of addondomain)
.htaccess
/image (directory)
.htaccess
1.png
UPDATE#2:
This works if I set .htaccess like this:
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.addondomain.com/image/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^addondomain.com$
RewriteRule ^(.*)$ http://www.addondomain.com/image/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addondomain\.maindomain\.com$
RewriteRule ^(.*)$ http://www.addondomain.com/image/$1 [R=301,L]
Is there any way to using some code (I am bit new) in place of image like
RewriteRule ^(.*)$ http://www.addondomain.com/.*$/$1 [R=301,L]
Whilst
.htaccessfiles (in general) are "inherited" along the filesystem path, mod_rewrite directives are not (by default). mod_rewrite directives in a.htaccessfile in a subdirectory will completely override those in the parent directory. This is why you are having to duplicate directives across multiple.htaccessfiles.However, you can "inherit" the parent directives with the
RewriteOptionsdirective in the.htaccessfile in the subdirectory. For example:This results in the mod_rewrite directives from the parent directory being "copied" into the current
.htaccessfile. This might help your current situation, however, relying too much on mod_rewrite inheritance is generally a bad idea as it can result in unexpected results that can be hard to debug. Note that directives are literally "copied", so you can find thatRewriteRulepatterns that might work in the parent directory, no longer match in the subdirectory - so it's not a magic fix. Also, having multiple.htaccessfiles along the filesystem path can make the system hard to manage. Better to have just one.htaccessfile in the root directory.You've not stated why you need an additional
.htaccessfile in the/imagessubdirectory. For domain canonicalisation (which is all that your directives are doing) it's certainly not required. Ideally you should include the necessary directives in the parent.htaccessfile.Personally, I would simply block (ie. 403 Forbidden) all requests via the
maindomain.com. This simplifies theaddondomain.com/.htaccessfile:Another way to avoid the
maindomain.comfrom interfering with theaddondomain.comis to create the directory (that the subdomain andaddondomain.compoint to) outside of themaindomain.comdirectory tree. For example: