利用php怎么強(qiáng)制用戶轉(zhuǎn)向www域名?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

// 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 "www.jb51.net" but some other
// applications send host names like "www.jb51.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 != 'www.jb51.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: https://www.jb51.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="https://www.jb51.net' . $url.'">Please click here</a>';
// stop the script execution here
exit;
}
// If the domain is www.jb51.net then go on with your PHP code
// of with your website...
// BTW: You need to replace jb51.net trough your own domain :-D看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。
網(wǎng)頁(yè)題目:利用php怎么強(qiáng)制用戶轉(zhuǎn)向www域名-創(chuàng)新互聯(lián)
分享鏈接:http://chinadenli.net/article6/dgheig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、服務(wù)器托管、標(biāo)簽優(yōu)化、網(wǎng)站內(nèi)鏈、網(wǎng)站營(yíng)銷、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容