Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP url functions and subdomains

Tags:

php

subdomain

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....

like image 513
Naeem Ul Wahhab Avatar asked Nov 27 '11 13:11

Naeem Ul Wahhab


2 Answers

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.

like image 126
Ahoura Ghotbi Avatar answered Oct 20 '22 04:10

Ahoura Ghotbi


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]
like image 21
Tom van der Woerdt Avatar answered Oct 20 '22 04:10

Tom van der Woerdt