1

I had a domain: mydomain.com pointing to a hosted apache server 'premium' account that can host multiple domains. I bought another domain: anotherDomain.com which I set up as an 'add on' domain with my web host. I can access the anotherDomain in several different ways:

mydomain.com/anotherDomain.com
anotherDomain.mydomain.com

and

anotherDomain.com

However, only when using the first method can I access 'generic' files on mydomain.com from anotherDomain.com (using relative addressing).

I was told there is a script I can write so anotherDomain.com can access 'root' files at mydomain.com, using relative addressing, but they cannot tell me how to do it. I've looked around the net, but although there are lots of similar sounding questions, I cannot find how to do it.

Just to restate the problem: I want to be able to access files in mydomain.com, just like I can when anotherDomain.com is accessed like: mydomain.com/anotherDomain.com, when it is accessed like: anotherDomain.mydomain.com or anotherDomain.com

Example: If I access anotherDomain.com using the URL mydomain.com/anotherDomain.com then, in the index.html for anotherDomain.com I can have:

<img src='../imgs/generic.jpg'/>

Which access the 'generic' image in the imgs folder for mydomain.com. Unfortunately, when I access this page using the URLs: anotherDomain.mydomain.com or anotherDomain.com, this doesn't work.

iPadDeveloper2011
  • 4,560
  • 1
  • 25
  • 36

4 Answers4

3

First of all, I assume you don't have access to your server's config files and thus have to deal with the restricted possibilities of .htaccess. If you have access there would be much better ways to handle this.

I would suggest to circumvent the problem. Have a look at the other answers and the comments. You went for a highly complex setup. You might get that working, but it will be a pain in the ass in the long run.

I will elaborate a bit and explain a few things in my answer.

As I understand you have the following file structure on your server:

public_html/
|
+-- index.html           // the index of mydomain.com
|
+-- imgs/
|   |
|   +-- generic.jpg
|
+-- anotherDomain.com/
    |
    +-- index.html       // index of anotherDomain.com

Suppose you browse http://anotherDomain.com/. When the browser tries to load generic.jpg it will create this URL: http://anotherDomain.com/../imgs/generic.jpg. This will, however, by almost every browser, be rewritten as http://anotherDomain.com/imgs/generic.jpg.

So you have to tell the server how to server this file.

  • You can create a rule as @anubhava suggests. If you access http://anotherDomain.com/imgs/* redirect it to the imgs dir on the virtual host http://mydomain.com/. This way the content will appear to belong to mydomain.com.

  • I would suggest creating a symlink instead, if you have the possibility.

    |
    +-- anotherDomain.com/
        |
        +-- index.html       // index of anotherDomain.com
        |
        +-- imgs --> ../imgs/
    

    This way you can access all the images easily. However, they will appear as content of anotherDomain.com. This can be seen as advantages or as already mentioned as disadvantage (search engines.) Creating a symlink can also sometimes be done by using PHP (symlink function) if your provider does not support it via its interface.

Scolytus
  • 16,338
  • 6
  • 46
  • 69
  • Unfortunately I don't choose how my webhost sets up my domains. I would like to mirror what I have on my local machine, where I am able to create/use generic shared files. I _really like_ the idea of creating a symlink folder. Seems like a *totally perfect* solution. *Unfortunately* there does not seem to be a way to do this using the interfaces provided by my web host. :...( – iPadDeveloper2011 Sep 24 '13 at 05:08
  • 1
    You could try to use PHP's file system function `symlink` ;) Sometimes it works. – Scolytus Sep 24 '13 at 06:25
  • And depending on the money you spent: Usually good providers have support for their customers. Are you sure you can't persuade them to edit your virtual host configuration? – Scolytus Sep 24 '13 at 06:27
  • I tried `'; else echo 'it failed!'; echo readlink($link); ?>` – iPadDeveloper2011 Sep 24 '13 at 06:40
  • Oh, and it said it worked! Also, `http://anotherDomain.com/genericJS/` lists the folder contents OK, so this seems to be superfine! :-D – iPadDeveloper2011 Sep 24 '13 at 06:41
  • Think about adding this info to your answer. Thank you! – iPadDeveloper2011 Sep 24 '13 at 06:45
  • I'm happy I could help you. I've added the note on PHP to the answer. – Scolytus Sep 24 '13 at 06:49
  • Thinking through the security implications of this is clearly something my web host has not done... Not sure if I should tell them or not. – iPadDeveloper2011 Sep 24 '13 at 07:03
  • :D You could just try to access another host's files. Maybe the setup is correct and you can't access files outside your root dir. – Scolytus Sep 24 '13 at 07:05
2

After wasting time trying to do this with .htaccess files, I finally worked out a way to do it in plain old html. In the header, before all other links, put:

<base href="http://mydomain.com/anotherDomain.com" />

And thats it. Remove this for development on your local machine. ;-)
Remember to be a bit careful when using the base tag.

Edit: I've found this causes other problems. For example, relative links go to http://mydomain.com/anotherDomain.com, rather than http://anotherDomain.com.
Adding .htaccess code to the http://mydomain.com root directory like:

RedirectMatch /anotherDomain.com(/)?$ http://anotherDomain.com

Solves this, but introduces other problems. Still looking for a good answer to this question... Anybody?

Community
  • 1
  • 1
iPadDeveloper2011
  • 4,560
  • 1
  • 25
  • 36
  • +1 for your answer. I wanted to take a shot at .htaccess rules but somehow I couldn't get clear understanding of your problem. Can you explain your problem with more details and examples please. – anubhava Sep 19 '13 at 09:48
  • Hi @anubhava. I've added an example to the question as you requested. – iPadDeveloper2011 Sep 20 '13 at 00:12
  • @anubhava. I *think* rewriting `anotherDomain.com` as `mydomain.com/anotherDomain.com` *should* work, but I have not had success with this myself. – iPadDeveloper2011 Sep 20 '13 at 00:17
  • So I think your `pulic_html/img` and `pulic_html/anotherDomain.com` folders are at same level right? Also DOCUMENT_ROOT for `anotherDomain.com` is also `pulic_html/anotherDomain.com/` is that correct? – anubhava Sep 20 '13 at 08:12
  • Yes. `root.php` `` outputs `/[..]/public_html/anotherDomain.com` – iPadDeveloper2011 Sep 22 '13 at 01:46
  • Ok so since you img dir is `/[..]/public_html/img` and your DOCUMENT_ROOT is `/[..]/public_html/anotherDomain.com` therefore img dir cannot be accessed from `http://aotherDomain.com` since it not side its DOCUMENT_ROOT tree. – anubhava Sep 22 '13 at 05:34
  • @anubhava. I just checked though, and *I am actually doing this currently*, using the `base` tag, as described in my answer. That part is working fine. – iPadDeveloper2011 Sep 23 '13 at 00:18
  • Yes that is correct using `` it will work since it prefixes `http://mydomain.com/anotherDomain.com` to all your images URLs. However as I explained there is no way to access these images using `http://anotherDomain.com` domain since `img` folder is out of bound for it (being outside DOCUMENT_ROOT). – anubhava Sep 23 '13 at 03:17
  • @anubhava Typing `http://anotherDomain.com` into the address bar currently works OK, due to the `` tag. Only problem is with relative links displaying `http://mydomain.com/anotherDomain.com`. Are you saying that there is no solution using `.htaccess` rewrite rules as `anotherDomain.com` cannot be rewritten as `mydomain.com/anotherDomain.com`? – iPadDeveloper2011 Sep 23 '13 at 04:29
  • Ok try my answer below. – anubhava Sep 23 '13 at 07:25
1

Try this code in your public_html/anotherDomain.com/.htaccess:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# for images only
RewriteCond %{HTTP_HOST} ^(www\.)?anotherDomain\.com$ [NC]
RewriteRule ^(imgs/.+)$ http://mydomain.com/anotherDomain.com/$1 [L,NC,R]

# or for all files
RewriteCond %{HTTP_HOST} ^(www\.)?anotherDomain\.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/anotherDomain.com/$1 [L,NC,R]

Remove (or comment out) your <base... tag.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I confirmed this doesn't work. What you are saying here--I think--is, rewrite `anotherDomain.com/imgs/...` as `mydomain.com/imgs/...` which is not what I wanted :-( Thanks very much for trying though :-) – iPadDeveloper2011 Sep 24 '13 at 04:45
  • But that is exactly you're doing via your `` – anubhava Sep 24 '13 at 04:48
  • Not quite :-). With the base tag, `./imgs` is still `mydomain.com/anotherDomain.com/imgs` (same as `anotherDomain.com/imgs`). The helpful thing about this tag is that `../imgs` refers to `mydomain.com/imgs`. I did test out your solution, following your instructions, just in case. – iPadDeveloper2011 Sep 24 '13 at 05:29
  • I am little confused. I thought `imgs` is real folder directly under `http://mydomain.com/` and at the same level as `anotherDomain.com/` folder? Is that not correct? – anubhava Sep 24 '13 at 05:31
  • Perhaps something like `RewriteCond %{HTTP_HOST} ^(www\.)anotherDomain\.com$ [NC] RewriteRule ^(.*)$ http://mydomain.com/anotherDomain.com$1 [L,NC,R]` would be equivalent? – iPadDeveloper2011 Sep 24 '13 at 05:37
  • Yes I was also going to say same based on your comments. Please try out edited answer.. – anubhava Sep 24 '13 at 05:39
  • Yes, I have `mydomain.com/imgs` but also `anotherDomain.com/imgs` the whole `mydomain.com/imgs` thing is just an *example* of a generic resource. In fact, I have generic `js` and `php` files. (I did add these to your code when testing). – iPadDeveloper2011 Sep 24 '13 at 05:41
  • Oh so you have 2 imgs folders one directly under `http://mydomain.com/` and another directly under `http://anothermydomain.com/` – anubhava Sep 24 '13 at 05:49
  • Thanks! Looks like this works. I did replace my relative urls with absolute ones, so will have to do further testing, but expect it will be fine! Thanks again. – iPadDeveloper2011 Sep 24 '13 at 05:53
  • Sorry :-( when initially testing this, I looked by mistake at the `localhost` site. When I actually test it, it doesn't work :-( Example: `"NetworkError: 404 Not Found - http://anotherDomain.com/js/utilities.js"` – iPadDeveloper2011 Sep 24 '13 at 06:23
  • When you directly enter `http://www.anotherDomain.com/js/utilities.js` in your browser does it not go to: `http://mydomain.com/anotherDomain.com/js/utilities.js` in your browser? Another mistake I made by not making `www.` optional (please see latest edit) – anubhava Sep 24 '13 at 06:55
0

Accessing the same content via several different domains is maybe not a great idea. This can make your content appear like duplicated contents from search engines. If you keep that idea you should at least try to register all your contents with a canonical url metatag or attribute.

Now I usually like relative urls, as always preficing url with the domain makes them harder to reuse via proxys (but when using ajax stuff, for example, relative url are not enough, you'll always end with some absolute url in some places). But here you use relative url with '../imgs/generic.jpg'. It means this url refers more to the way you've been putting the files on your server than to something which is meaningful. It could be meaningfull on a single domain, but to share stuff between domains it's not. What if some day you need to move the new domain files and directory root on another place (on another server?). From the user (and bots and search engines) your domains are not related. Any proxy that you do not own would request the asset several time if it is asked via subdomain.mydomain.com/foo.jpg and subdomain.com/foo.jpg, so trying to share this stuff from your domains is not meaningful for the rest of the web, it's just for your own managment, on your side. So feel free to make it simple to manage.

You'd better manage your url in a way where the url means something to you, like '/common/imgs/generic.jpg' and '/site/imgs/custom.jpg'. Then on the domain Virtualhosts, server-side, you can work on the mapping url->directory & files. This mapping can be set via Alias, AliasMatch, and of course mod_rewrite directives.

For example a simple

Alias /common /path/to/mydomain/assets

Would allow you to map all the mydomain.com assets on the Virtualhost containing this instruction. The day you will decde to move all the things on your webserver you'll simply have to rework the apache rewrites and alias, and not all you code url management.

regilero
  • 29,806
  • 6
  • 60
  • 99
  • Thanks for your answer. Unfortunately I am using a commercial web hosting service, and I don't have access to Apache configuration, and `Alias` cannot be used in `.htaccess` files. – iPadDeveloper2011 Sep 24 '13 at 04:53
  • Yes, but mod_rewrite can do the same and even more. The basic usage of a `RewriteRule` is usually the same job as the stuff done by `Alias`. Mapping a query path to a filesystem path. – regilero Sep 24 '13 at 08:31