PHP url functions and subdomains

为君一笑 提交于 2019-12-04 08:44:00

问题


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


回答1:


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.




回答2:


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]


来源:https://stackoverflow.com/questions/8286054/php-url-functions-and-subdomains

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!