php強(qiáng)制用戶(hù)轉(zhuǎn)向www域名的方法
本文實(shí)例講述了php強(qiáng)制用戶(hù)轉(zhuǎn)向www域名的方法。分享給大家供大家參考。具體分析如下:
有時(shí)候網(wǎng)站的www域名和非www域名都能訪(fǎng)問(wèn)網(wǎng)站,但是這樣不利于搜索引擎的收錄,會(huì)分散網(wǎng)頁(yè)的權(quán)重,所以希望用戶(hù)訪(fǎng)問(wèn)非www的域名時(shí)通過(guò)301永久重定向到www域名,例如用戶(hù)訪(fǎng)問(wèn)jb51.net會(huì)直接轉(zhuǎn)向m.fzitv.net,本php代碼考慮了無(wú)法通過(guò)head重定向的情況,會(huì)在頁(yè)面上輸出鏈接,讓用戶(hù)點(diǎn)擊。
// Install info.:
// Copy and paste these lines into your default index.php or
// the file that get's called if a visitor comes on your
// website...
// read the host from the server environment
$host = $_SERVER["HTTP_HOST"];
// fix host name - we never now... ;-)
$host = strtolower($host);
$host = trim($host);
// This is important:
// Webbrowsers like Firefox are doing their request without
// the port number like "m.fzitv.net" but some other
// applications send host names like "m.fzitv.net:80"
$host = str_replace(':80', '', $host);
$host = trim($host);
// if the host is not starting with www. redirect the
// user to the same URL but with www :-)
if ($host != 'm.fzitv.net'){
// You an also change the "!=" to "==", if you want to force
// the user to use the domain name without the www.
// send status header, so that search engines or other services
// detect that this is a permanent redirect and not a temporary
header('HTTP/1.1 301 Moved Permanently');
// read the URL the user requested:
$url = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : '';
// redirect the user to the new destination:
header('Location: http://m.fzitv.net' . $url);
// Convert "special" chars -- cause we never now... ;-)
$url = htmlspecialchars($url);
// "fallback" link, if the browser is not supporting header redirects
print '<a href="http://m.fzitv.net' . $url.'">Please click here</a>';
// stop the script execution here
exit;
}
// If the domain is m.fzitv.net then go on with your PHP code
// of with your website...
// BTW: You need to replace jb51.net trough your own domain :-D
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
php實(shí)現(xiàn)的中文分詞類(lèi)完整實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的中文分詞類(lèi),結(jié)合完整實(shí)例形式分析了php基于字符串的遍歷、轉(zhuǎn)換、運(yùn)算等技巧實(shí)現(xiàn)中文分詞功能的具體方法,需要的朋友可以參考下2017-02-02
PHP遍歷某個(gè)目錄下的所有文件和子文件夾的實(shí)現(xiàn)代碼
本篇文章是對(duì)PHP遍歷某個(gè)目錄下的所有文件和子文件夾的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP手機(jī)號(hào)碼歸屬地查詢(xún)代碼(API接口/mysql)
本文來(lái)介紹一下關(guān)于手機(jī)號(hào)碼歸屬地實(shí)現(xiàn)方法,我們可以利用api接口與mysql+php來(lái)實(shí)例有需要的同學(xué)看看2012-09-09
PHP中實(shí)現(xiàn)漢字轉(zhuǎn)區(qū)位碼應(yīng)用源碼實(shí)例解析
PHP里如何實(shí)現(xiàn)漢字轉(zhuǎn)區(qū)位碼這個(gè)問(wèn)題一直困擾這大多程序員,那么下面這個(gè)源碼實(shí)例相信能給大家?guī)?lái)很大的幫助。2010-06-06
php結(jié)合ACCESS的跨庫(kù)查詢(xún)功能
有時(shí)候我們?cè)陂_(kāi)發(fā)一些小程序或簡(jiǎn)單功能的管理系統(tǒng),又沒(méi)有mysql數(shù)據(jù)庫(kù)的情況下,就可以使用access了,另外有時(shí)需要在兩個(gè)或三個(gè)數(shù)據(jù)庫(kù)的表中也可通過(guò)ACCESS的跨庫(kù)查詢(xún)功能實(shí)現(xiàn)2015-06-06

