国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php強制用戶轉(zhuǎn)向www域名的方法

2024-05-04 23:36:38
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php強制用戶轉(zhuǎn)向www域名的方法,可實現(xiàn)模擬301重定向的功能,并且針對無法head重定向的情況輸出鏈接,需要的朋友可以參考下

本文實例講述了php強制用戶轉(zhuǎn)向www域名的方法。分享給大家供大家參考。具體分析如下:

有時候網(wǎng)站的www域名和非www域名都能訪問網(wǎng)站,但是這樣不利于搜索引擎的收錄,會分散網(wǎng)頁的權(quán)重,所以希望用戶訪問非www的域名時通過301永久重定向到www域名,例如用戶訪問vevb.com會直接轉(zhuǎn)向m.survivalescaperooms.com,本php代碼考慮了無法通過head重定向的情況,會在頁面上輸出鏈接,讓用戶點擊。

 

 
  1. // Install info.: 
  2. // Copy and paste these lines into your default index.php or 
  3. // the file that get's called if a visitor comes on your  
  4. // website... 
  5. // read the host from the server environment 
  6. $host = $_SERVER["HTTP_HOST"]; 
  7. // fix host name - we never now... ;-) 
  8. $host = strtolower($host); 
  9. $host = trim($host); 
  10. // This is important:  
  11. // Webbrowsers like Firefox are doing their request without 
  12. // the port number like "m.survivalescaperooms.com" but some other  
  13. // applications send host names like "m.survivalescaperooms.com:80"  
  14. $host = str_replace(':80'''$host); 
  15. $host = trim($host); 
  16. // if the host is not starting with www. redirect the  
  17. // user to the same URL but with www :-) 
  18. if ($host != 'm.survivalescaperooms.com'){ 
  19. // You an also change the "!=" to "==", if you want to force  
  20. // the user to use the domain name without the www.  
  21. // send status header, so that search engines or other services 
  22. // detect that this is a permanent redirect and not a temporary 
  23. header('HTTP/1.1 301 Moved Permanently'); 
  24. // read the URL the user requested: 
  25. $url = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : ''
  26. // redirect the user to the new destination: 
  27. header('Location: http://m.survivalescaperooms.com' . $url); 
  28. // Convert "special" chars -- cause we never now... ;-) 
  29. $url = htmlspecialchars($url); 
  30. // "fallback" link, if the browser is not supporting header redirects 
  31. print '<a href="http://m.survivalescaperooms.com' . $url.'">Please click here</a>'
  32. // stop the script execution here 
  33. exit
  34. // If the domain is m.survivalescaperooms.com then go on with your PHP code  
  35. // of with your website... 
  36. // BTW: You need to replace vevb.com trough your own domain :-D 

希望本文所述對大家的php程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凉山| 姜堰市| 苗栗市| 波密县| 枞阳县| 葫芦岛市| 花垣县| 无为县| 景谷| 客服| 临颍县| 白山市| 台中县| 芮城县| 广元市| 仲巴县| 苏尼特左旗| 南开区| 弋阳县| 当阳市| 岢岚县| 土默特左旗| 安福县| 漳州市| 仙游县| 湖州市| 盘锦市| 沙湾县| 扶风县| 黔西县| 武平县| 德昌县| 徐汇区| 巫溪县| 房产| 衡水市| 中西区| 南投县| 且末县| 平泉县| 张北县|