2012-04-09 78 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

相關問題