2012-04-09 75 views
0

我们网站中的很多页面都使用旧的子域名来显示图像,CSS和JavaScript文件。我从新手开发人员那里继承了这个项目,他们没有使用任何通用的设计模板。结果是数百页静态引用内容的例子是:指向站点文件夹的外部域名和子目录

http://static.foo.bar/css/sample.css 
http://static.foo.bar/brochure/css/sample.css 
http://static.foo.bar/company/newsletter/js/common.js 
http://static.foo.bar/images/version2/header.jpg 

...和其他数百个位置。我需要将它们全部指向主域而不是子域,而不必在.htaccess文件中为每个域创建规则。所以:

http://static.foo.bar/css/sample.css 
should point to: 
http://www.foo.bar/css/sample.css 

http://static.foo.bar/brochure/css/sample.css 
should point to: 
http://www.foo.bar/brochure/css/sample.css 

http://static.foo.bar/company/newsletter/js/common.js 
should point to: 
http://www.foo.bar/company/newsletter/js/common.js 

http://static.foo.bar/images/version2/header.jpg 
should point to: 
http://www.foo.bar/images/version2/header.jpg 

我知道这是可能的只有我不是服务器的人。任何帮助将非常感激。

回答

0

与此内容创建.htaccess文件中的文档根:

添加到您的服务器配置,%{HTTP_HOST}检查不从.htaccess工作:

<IfModule mod_rewrite.c> 
# bad matching: RewriteCond %{HTTP_HOST} !^static\.foo\.bar [NC] 
# 
# correction, matching everything other than www.foo.bar : 
RewriteCond %{HTTP_HOST} !^www\.foo\.bar$ [NC] 
RewriteCond %{HTTP_HOST} !^$ 
RewriteRule ^/(.*)   http://www.foo.bar/$1 [L,R] 
</IfModule> 

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url

更新: 尽管如此,如果它托管在同一台服务器上,只是广告d

ServerAlias static.foo.bar 

到www.foo.bar配置也提供静态内容。根据反馈

更新#2:

这应该工作(工作在我的电脑)在.htaccess文件中,这会将所有的请求来static.foo.bar/*www.foo.bar/*

RewriteCond %{HTTP_HOST} ^static\.foo\.bar [NC] 
RewriteRule ^(.*)$   http://www.foo.bar/$1 [R,L] 

没有, ServerAlias命令仅适用于VirtualHost配置:

http://httpd.apache.org/docs/2.2/mod/core.html#serveralias

+0

嗨。感谢您的建议。这是否适用于子域的根目录及其子目录? – 2012-04-09 13:39:13

+0

这将使用相同的整个路径将请求从一个主机名重定向到另一个主机名,不管路径是什么,也不管你在问题中写的是什么。 – 2012-04-09 13:43:55

+0

虽然没有工作。我尝试将http://www.foo.bar/nonexistent/$1 [L,R]替换为应该导致错误404的测试,但所有css和图像仍然成功从子域中获取。 – 2012-04-09 15:19:40

相关问题