天天看點

apache利用.htaccess進行反向域名綁定

[size=x-large]一、需求:[/size]

因公司帶寬問題,是以想把靜态資源移置CDN上(阿裡或騰訊),但是某些前台js不支援跨域操作(createjs),但是頁面檔案為jsp頁面,未移置到CDN上,createjs在對頁面中的跨域圖檔就無權修改,是以想都在同一域名進入,頁面檔案正常通路apache下的,指定靜态檔案定位到CDN位址上。

[size=x-large]二、解決方案[/size]

1. 沒有用到createjs的配置URL為CDN上的資源。

2. 用到createjs的配置URL為域名映射的apache下對應的URL。

[size=x-large]三、apache的配置[/size]

這裡隻是指定apache下某目錄資源被通路跳轉到CDN上請求,不是是以請求。如果是是以,可以直接配置在httpd.conf中。

[size=medium]1. 配置.htaccess,檔案放在被通路的目錄下[/size]

[quote][url]http://www.jb51.net/article/25476.htm[/url]

[url]http://www.freehao123.com/park-domains/[/url][/quote]

[size=medium]2. 配置httpd.conf[/size]

[quote][url]http://www.jb51.net/article/47889.htm[/url][/quote]

[size=medium]3. windows下建立.htaccess檔案方法[/size]

1) 打開Windows内建的記事本;

2) 選取另存新檔案“檔案->另存為”;

3) 在另存為的“儲存類型”下拉選單中選取“所有檔案”類型,可以選擇utf-8,也可以是ansi

4) 這時就可在“檔案名”中輸入.htaccess;

5) 選取要另存的路徑再按下儲存,一個.htaccess 檔案就新增完成。

[quote][url]http://blog.sina.com.cn/s/blog_558839ae0100ev90.html[/url][/quote]

如:目錄結構

DocumentRoot "D:/Develop/Apache2.2/test"

D:\Develop\Apache2.2\test\test 不需要跳轉的目錄

D:\Develop\Apache2.2\test\test\index.html apache下的此檔案可以正常通路

D:\Develop\Apache2.2\test\udp\games\jianren2 為需要跳轉到CND的目錄

D:\Develop\Apache2.2\test\udp\games\jianren2\index.html apache下的此檔案不能被通路,而是直接跳轉到CDN,因為AllowOverride All,同時目錄下有.htaccess檔案指定了跳轉規則。

效果:

[img]http://dl2.iteye.com/upload/attachment/0114/3097/2c01a6a2-b0f4-395a-a1a9-ec61a853c42a.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0114/3099/74f86f3c-cf9b-301a-9657-f4a90bcf227e.jpg[/img]

已成功跳轉的頁面:

[img]http://dl2.iteye.com/upload/attachment/0114/3103/3a9d3f6e-4f7c-3b9c-b634-2ee0aa819326.jpg[/img]

apache下正常通路的頁面:

[img]http://dl2.iteye.com/upload/attachment/0114/3101/ee462f17-7655-3dac-8e80-c32343b76377.jpg[/img]

apache下沒有配置.htaccess時被通路到的頁面

[img]http://dl2.iteye.com/upload/attachment/0114/3134/3e956627-f17e-3c04-8bff-75d8479e09cf.jpg[/img]