5

I have a website thenoblesite.com. It has some subdomains i.e.

download.thenoblesite.com
wallpaper.thenoblesite.com
etc.

Pages for subdomains are present in the main htdocs folder i.e.

httpdocs/download <- download.thenoblesite.com
httpdocs/wallpaper <- wallpaper.thenoblesite.com

Problem is that I am using $_SERVER['DOCUMENT_ROOT'] . '/css'; for css folder and other common folders(graphics, includes, script etc). However in the subdomain page download.thenoblesite.com, $_SERVER['DOCUMENT_ROOT'] will refer to download.thenoblesite.com root folder, not the main thenoblesite.com root folder where css,graphics and includes folders are present.

I have to place the same graphics, css and includes folders separately on all subdomains. Every time I update the website I have to copy the common folders to all subdomains folders.

Another related problem is that i have to use absolute linking for the large sized downloads folder for e.g. VLC media player I have to use thenoblesite.com/download/vlc.exe or i also have to duplicate the large size download folder in all subdomain folders. This method unnecessarily increases the website size , creates confusion when I update the site and doesn't look good programming practise. Is there any possible PHP solution so that i can use the same css, images, downloads and includes folder for all subdomains....

mauris
  • 42,982
  • 15
  • 99
  • 131
Naeem Ul Wahhab
  • 2,465
  • 4
  • 32
  • 59

2 Answers2

7

I am not sure if this is something you might be interested in, but you could always create a new subdomain and call it something like style.[domain] and create a new variable in your config file and point it to that. this way you have all the images and css files etc stored in one place and if your traffic spikes you can always move that subdomain to a CDN etc so its really customizable.

UPDATE

ok so you can simply use a new variable in your config file like below :

$_config['http'] = 'http://www.yousite.com/';

now you can just use this variable to point to all your downloads etc on the main site rather than each pointing to the subdomain's folder. and if you want to be more flexible you can also add a few more css or js folders like :

$_config['http'] = 'http://www.yousite.com/';
$_config['css'] = $_config['http']."css";
$_config['js'] = $_config['http']."js";

the solution above will also help you if you decided to move the files around or just move a certain folder around etc. this is a good practice if you can adopt it.

Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65
  • 2
    I think you didn't get my point as my question is not clear. I will clarify my question. – Naeem Ul Wahhab Nov 27 '11 at 15:11
  • 2
    Thanks a lot for the reply. Your answer is somehow related to my problem and will sure help me after my problem is solved. I have tried to clarify my question. – Naeem Ul Wahhab Nov 27 '11 at 15:18
  • 3
    Actually, this is a very good solution to your problem - and it's definitely the commonly accepted one, since it will also mean you don't have to load images twice (caches). – Tom van der Woerdt Nov 27 '11 at 15:19
  • 2
    @capricorn to be honest this solution will fix all your problems, if you wrote the script yourself you should always use a variable to point to your files etc. I have updated my answer to help you in future when you want to write your scripts, but I guess it isnt too late to also change your current script. – Ahoura Ghotbi Nov 27 '11 at 15:31
  • @TomvanderWoerdt I appreciate your help, but I want to be careful about changing the server config file. Could you guide me to any resource, Or tell me the keywords by which I could search for the basics about this technique. – Naeem Ul Wahhab Nov 27 '11 at 15:34
  • 1
    http://www.riyaz.net/blogging/setup-own-cdn/890/ - not at all config related though. Also, editing htaccess files is unrelated to the server config. – Tom van der Woerdt Nov 27 '11 at 15:35
  • @TomvanderWoerdt hmmm thanks for your help and time freind. I am reading the pages you recommended. :-) – Naeem Ul Wahhab Nov 27 '11 at 15:39
  • @AhouraGhotbi thanks alot friend :-) You really help me to figure out this problem. – Naeem Ul Wahhab Nov 27 '11 at 15:41
  • @AhouraGhotbi Yes I will change the current script soon. As it will be helpful a lot for future updates to my site. Thankyou. Have good time :-) – Naeem Ul Wahhab Nov 27 '11 at 15:45
  • 1
    @AhouraGhotbi You may have received my appreciation for this answer. More is yet to come. thanks for help :-) – Naeem Ul Wahhab Dec 08 '11 at 15:28
  • In this case I would rather prefer another subdomain like static.thenoblesite.com and go on with answer from @AhouraGhotbi – Manuel Rauber Dec 15 '11 at 07:24
  • 1
    You helped me years back with a good solution. m still. thankful to you brother. +1 for you – Naeem Ul Wahhab Aug 28 '16 at 07:48
1

You might be able to use an alias in htaccess (or the server config) :

Alias /images /home/username/public_html/images

If that's not possible, you could rewrite all requests to /images via htaccess:

# Untested - should get you on the right track though
RewriteEngine On
RewriteRule ^/images/(.*)$ http://yourdomain.com/images/$1 [R=301,L]
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • 2
    thanks for help. Actually I haven't used htaccess or server config methods before. Can you give me a link so that I can know some basics about editing htaccess or server configurations. – Naeem Ul Wahhab Nov 27 '11 at 15:25
  • 1
    You can simply put a file called ".htaccess" in your root directory. Do note that this will only work for Apache servers. I have never read a single tutorial about htaccess, so I'm going to point you at the wikipedia page: http://en.wikipedia.org/wiki/Htaccess – Tom van der Woerdt Nov 27 '11 at 15:26