Mobile site on subdomain: accessing images etc. on main domain

I have decided to create a mobile site on a subdomain of my main site. (I’m aware there are endless arguments about having one site or two; let’s not get into that here).

The subdomain (http://subdomain.maindomain.co.uk) is of course also a sub-directory of the main domain.

To save work (and potential errors) I want to access images and other files from the main site rather than duplicate them all.
From what I’ve read (and tried) it seems that this has to be done with a full url, as in:

<img src='http://maindomain.co.uk/images/imagename.jpg' />

whereas if the subdomain were just a sub-directory I would use:

<img src='../../images/imagename.jpg' />

(from a sub-directory of the subdomian)
It seems to me that using the full URL must be slower, so before going ahead I’d like to check that there is no other (better) way.
I have looked briefly at mod_rewrite, but perhaps I got the code wrong, as it didn’t work. But even if it did, it’s still effectively going to be using the full URL, isn’t it ?

I would use the full URL, if you want to keep it a sub-domain.

You can use rewrite to point it elsewhere, but lots of stuff complains about it (like Google). It also slows your site down because then every single image basically winds up with a redirect (since HTTP Requests take the most time when loading a page, doubling the number of requests on your page will slow it down a lot).

Thank you for your reply.

I am using the full URL at the moment. I just wanted to check that there was no alternative. (i.e. a way of using a relative URL, as set out in my original post):

The subdomain (http://subdomain.maindomain.co.uk) is of course also a sub-directory of the main domain.

So one would hope it could be accessed as:

http://maindomain.co.uk/subdomain

But it seems not. Which makes me wonder if there’s all that much benefit in having the mobile site as a sub-domain. Why not just as a sub-directory ? There are various ways of automatically directing mobile browsers to the mobile site (some better than others), so perhaps it doesn’t greatly matter ?

Personally I usually just make it use the same URL and serve appropriate content.

When you use a sub-domain, even if it is actually a sub-folder, the browser doesn’t know this. Since the browser doesn’t know this, it can’t make guesses. So, if you use a relative link, it’ll always be relative of the current URL.

Since the main site stuff exists outside of the sub-folder the sub-domain points to, it can’t reach it. You can only go down from a sub-domain (or domain in general), not up.

So, something like this:


http://sub.domain.com/../cookie.gif

Is impossible, because it can’t go above http://sub.domain.com/, and it doesn’t know that it’s the same as http://domain.com/sub/.

Thank you. Yes, I understand the problem as usually explained.

However, there are certainly some circumstances where it’s not true. I routinely put data ‘above the root’, for security. There’s no problem accessing it from within the code (PHP, usually, in my case) but it can’t be accessed by someone typing a URL into their browser address bar. So the (partial) directory structure on my server is:

/public_html/index.php

and I can access stuff at:

/data/datafiles.txt

where ‘public_html’ and ‘data’ are directories at the same level, both subdirectories of ‘/’. (I’m always unsure which level is truly the ‘root’. Certainly in my FTP I can go no higher than the ‘/’).
Why does this NOT apply simply because a directory has been called a subdomain ? I realise I may have to accept the answer ‘because it does’ !

You’re looking at it from two sides of the coin.

On the PHP end, you are able to go above the root because you are on the server. Your server knows where it actually is relative to the rest of the files, so going up is no problem (because something like /var/www/…/private can be resolved to /var/private).

On the HTML end however, you are on the client’s side. They don’t know there is anything above http://sub.domain.com/, so they can’t convert http://sub.domain.com/.../private to anything.

Thank you, Samanime. I begin to see the difference now.

There doesn’t seem to be a great advantage in having a subdomain at all, since I can more easily link to ‘domain.co.uk/subdirectory’ than to ‘subdomain.domain.co.uk’ (where subdomain and subdirectory are in fact the same). It seems to me either way will be transparent to the mobile browser which gets redirected, and the ‘subdirectory’ way will be quicker (although maybe not much).

Is this right, or am I missing something ?

No, I think that’s pretty accurate.

Generally, if you want a sub-site to access common content directly (i.e., on the same box), a sub-directory is the best way to go.

Usually with sub-domains, the site is either fully independent, or is drawing on files and information that is stored in a third location (like a static file/“media” server or an external database).

Thanks again. If nothing else it’s good to know one is doing the right thing for the right reasons.