2

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]
MrWhite
  • 43,179
  • 8
  • 60
  • 84
user3384093
  • 45
  • 1
  • 8
  • Can you include your directory structure please. Is your `addondomain.com` pointing to a subdirectory of the `maindomain.com` document root? `.htaccess` files are inherited along the filesystem path, however, mod_rewrite directives are not inherited by default. It is generally preferable to avoid multiple `.htaccess` files in subdirectories. – MrWhite Nov 30 '16 at 10:19
  • "Some how, image folder cut off." - Your rules in the image subdirectory don't include the "image" folder in the substitution, so that would be why the image folder is being "cut off". It would be easier (in `.htaccess`) to make use of the directory-prefix, rather than explicitly specifying absolute URLs. – MrWhite Nov 30 '16 at 10:22
  • " It would be easier (in .htaccess) to make use of the directory-prefix, rather than explicitly specifying absolute URLs" How to use directory-prefix, can you add some code – user3384093 Nov 30 '16 at 10:51
  • Why do you need to put another `.htaccess` in a subdirectory of the `addondomain.com`? Presumably you are doing more than domain canonicalisation? Do you need to redirect everything? For instance, personally, I would simply 403 all requests for `maindomain.com/addondomain.com/`. Do you have other subdomains that you need to account for? – MrWhite Nov 30 '16 at 16:31
  • (Sorry, my comment about the directory-prefix doesn't apply here, since you are canonicalizing the domain.) – MrWhite Nov 30 '16 at 16:36

1 Answers1

0

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
MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • The above shouldn't be triggering a 403 when accessing via `addondomain.com`? (The `RewriteOptions` directive was just an example, I would not recommend using that in your current config - it shouldn't be necessary. But you've not explained what exactly you are doing with this setup? Are you _only_ canonicalising the domain?) – MrWhite Dec 01 '16 at 08:56