Addon domain and .htaccess file redirect

3.3k Views Asked by At

I have two domains:

  1. maindomain.com
  2. www.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]
1

There are 1 best solutions below

1
MrWhite On

Whilst .htaccess files (in general) are "inherited" along the filesystem path, mod_rewrite directives are not (by default). mod_rewrite directives in a .htaccess file in a subdirectory will completely override those in the parent directory. This is why you are having to duplicate directives across multiple .htaccess files.

However, you can "inherit" the parent directives with the RewriteOptions directive in the .htaccess file in the subdirectory. For example:

RewriteOptions inherit

This results in the mod_rewrite directives from the parent directory being "copied" into the current .htaccess file. 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 that RewriteRule patterns that might work in the parent directory, no longer match in the subdirectory - so it's not a magic fix. Also, having multiple .htaccess files along the filesystem path can make the system hard to manage. Better to have just one .htaccess file in the root directory.

You've not stated why you need an additional .htaccess file in the /images subdirectory. 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 .htaccess file.

Personally, I would simply block (ie. 403 Forbidden) all requests via the maindomain.com. This simplifies the addondomain.com/.htaccess file:

RewriteEngine On

# Block all access via maindomain.com
RewriteCond %{HTTP_HOST} \.maindomain\.com$ [OR]
RewriteRule ^ - [F]

# Canonicalise www.addondomain.com
RewriteCond %{HTTP_HOST} =addondomain\.com$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

# Any additional rewrites relating to the /image subdirectory (for example)
#RewriteRule ^image/(.+) /get-image.php?file=$1 [L]

Another way to avoid the maindomain.com from interfering with the addondomain.com is to create the directory (that the subdomain and addondomain.com point to) outside of the maindomain.com directory tree. For example:

/public_html
    .htaccess
    home.php (page of main domain)
/addondomain.com (directory)
    home.php (page of addondomain)
    .htaccess
    /image (directory)
        1.png