html中如何重定向域名,域名如何进行重定向
何為地址重寫,域名如何進行重定向?
地址重寫,獲取一個來訪的URL請求,然后改寫成服務器可以處理的另一個URL的過程
功能:www.baidu.com------>https://www.baidu.com
優(yōu)點:縮短URL,隱藏實際路徑,提高安全性;
易于用戶記憶和鍵入;易于被搜索引擎收錄
常見網(wǎng)站應用場景:
當網(wǎng)站文件移動或文件目錄名稱發(fā)現(xiàn)改變,出于SEO需要,你需要保持舊的URL
網(wǎng)站改版了,網(wǎng)站導航和鏈接發(fā)生變化,為了繼續(xù)持有原鏈接帶來的流量,需要保持舊的URL
rewrite語法:rewrite?舊地址(正則regex)??新地址(跳轉后的URL)【選項flag】
if?(條件){...}
[選項flag]:
last:停止執(zhí)行其他重寫規(guī)則,根據(jù)URL繼續(xù)搜索其他location,地址欄不改變
break:停止執(zhí)行其他重寫規(guī)則,完成本次請求
redirect:302臨時重定向,地址欄改變,爬蟲不更新URL
permament:301永久重定向,地址欄改變,爬蟲更新URL
正則表達式匹配模式
區(qū)分大小寫匹配:~
不區(qū)分大小寫匹配:~*
區(qū)分大小寫不匹配:!~
不區(qū)分大小寫不匹配:!~*
判斷文件是否存在:-f
判斷目錄是否存在:-d
判斷文件是否可執(zhí)行:-x
判斷文件、目錄、連接是否存在:-e
頁面跳轉:訪問a.html----->自動跳轉到b.html???/usr/local/nginx/conf/nginx.conf
server?{listen?80
server_name?localhost;
location?/?{root?html;
index?index.html?index.htm;
rewrite?/a.html?/b.html??redirect;}}???//選項redirect可有可無,區(qū)別為有選項時地址欄改變,不使用此選項則地址欄不變
服務端Proxy??#?echo?"BB"?>?/usr/local/nginx/html/b.html
#?/usr/local/nginx/sbin/nginx??-s??reload
客戶端client??#?firefox?http://192.168.4.25/a.html
網(wǎng)站跳轉:訪問192.168.4.5跳轉到www.tmooc.cn???/usr/local/nginx/conf/nginx.conf
注:寫在location內對根文件夾生效,寫在location外對所有文件生效
seerver?{listen?80;
server?_?name?localhost;
rewrite?^/?http:www.tmooc.cn;}?????????????//^/也可寫成^/.*,模糊匹配
location?/?{root?html;
index??index.html?index.htm;}}
服務端Proxy???#?/usr/local/nginx/sbin/nginx??-s??reload
客戶端client??#?firefox?http://192.168.4.25
附加:訪問舊的網(wǎng)站/下面子頁面,跳轉到新的網(wǎng)站/下相同頁面
rewrite?^/(.*)?http://www.jd.com/$1;?????//(.*)?保留和復制;$1在nginx代表粘貼
/usr/local/nginx/conf/nginx.conf
server?{listen???????80;
server_name??localhost;
rewrite?^/(.*)?http://www.tmooc.cn/$1;????????????//在location外部
location?/?{root???html;
index??index.html?index.htm;}}
服務端Proxy?? #?/usr/local/nginx/sbin/nginx??-s??reload
客戶端client #?firefox??http://192.168.4.5
www.tarena.com--->bbs.tarena.com????/usr/local/nginx/conf/nginx.conf
seerver?{listen?80;
server?_?name?www.tarena.com;
location?/?{root?html;
index?index.html?index.htm;
rewrite?^/(.*)?http://bbs.tarena.com/$1;}}???//在location內部
*.jpg或*.gif?---->?logo.png????????/usr/local/nginx/conf/nginx.conf
location?/?{root?html;
index?index.html?index.htm;
rewrite?\.(gif|jpg)$?/logo.png;}
訪問不同瀏覽器的相同鏈接返回不同的頁面?????/usr/local/nginx/conf/nginx.conf
注:if($http_user_agent?~*??瀏覽器類型(firefox/uc/.....)){
rewrite??^/(.*)???/瀏覽器類型文件夾/文件;}
寫在location內是死循環(huán)(報錯http500),寫在location外是正常
在服務端Proxy創(chuàng)建網(wǎng)頁目錄以及對應的頁面文件:
#?echo?"I?am?Normal?page"?>?/usr/local/nginx/html/test.html??//正常頁面
#?mkdir??-p??/usr/local/nginx/html/curl/
#?echo?"I?am?is?curl?page"?>?/usr/local/nginx/html/curl/test.html
#?cp?/usr/share/backgrounds/gnome/Road.jpg?>?/usr/local/nginx/curl/test.jpg
修改配置文件
seerver?{listen?80;
server?_?name?www.tarena.com;
location?/?{root?html;
index?index.html?index.htm;}
if?($http_user_agent?~*?curl)?{???????????????????//識別客戶端curl瀏覽器
rewrite?^/(.*)?/curl/$1?break;}}
服務端Proxy?? #?/usr/local/nginx/sbin/nginx??-s??reload
客戶端client #?firefox??http://192.168.4.5/test.html
#?curl?????http://192.168.4.5/test.html
#?curl?????http://192.168.4.5/test.jsp
訪問資源不存在時,重定向到指定頁面???????/usr/local/nginx/conf/nginx.conf
注:if(!-e??$request_filename){
rewrite??^/??http://域名/文件;}
seerver?{listen?80;
server?_?name?www.tarena.com;
location?/?{root?html;
index?index.html?index.htm;}
if(!?-e?$request_filename){
rewrite?^/http://www.tarena.com/none.html;}}
服務端Proxy?? #?/usr/local/nginx/sbin/nginx??-s??reload
客戶端client #?firefox??http://192.168.4.5/a.mp4
你也許會喜歡如下的文章?
(責任編輯:365天天網(wǎng))
總結
以上是生活随笔為你收集整理的html中如何重定向域名,域名如何进行重定向的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [ARC072C]Alice in li
- 下一篇: jquery实现图片上传