本文目錄一覽:
- 1、php 函數重定向 Warning: Cannot modify header information – headers already sent by (output
- 2、怎麼做301轉向,asp,php301重定向跳轉代碼
- 3、thinkphp內核程序,無法重定向
- 4、如何用 PHP 實現 302 重定向到其他 URL
php 函數重定向 Warning: Cannot modify header information – headers already sent by (output
檢查以下兩個方面:
一、在文件的第一個?php之前不得有任何內容,包括空白、空行
二、在header(‘Location:news_list.php?message=$message’);語句之前不得有任何的echo或者其它輸出內容的語句
滿足以上兩點的情況下,就不會報告你這個錯誤。
怎麼做301轉向,asp,php301重定向跳轉代碼
301跳轉代碼全集(ASP|PHP|JSP|.NET)
1、IIS下301設置
Internet信息服務管理器 – 虛擬目錄 – 重定向到URL,輸入需要轉向的目標URL,並選擇“資源的永久重定向”。
2、ASP下的301轉向代碼
%@ Language=VBScript %
%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “”
%
3、ASP.Net下的301轉向代碼
script runat=”server”
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,””);
}
/script
4、PHP下的301轉向代碼
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: ”);
exit();
5、CGI Perl下的301轉向代碼
$q = new CGI;
print $q-redirect(””);
6、JSP下的301轉向代碼
%
response.setStatus(301);
response.setHeader( “Location”,“” );
response.setHeader( “Connection”,“close” );
%
7、Apache下vhosts.conf中配置301轉向
為實現URL規範化,SEO通常將不帶WWW的域名轉向到帶WWW域名,vhosts.conf中配置為:
VirtualHost *:80
ServerName
DocumentRoot
/VirtualHost
VirtualHost *:80
ServerName xxx.com
RedirectMatch permanent ^/(.*)
/VirtualHost
8、Apache下301轉向代碼
新建.htaccess文件,輸入下列內容(需要開啟mod_rewrite):
1)將不帶WWW的域名轉向到帶WWW的域名下
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.com [NC]
RewriteRule ^(.*)$ [L,R=301]
2)重定向到新域名
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ [L,R=301]
3)使用正則進行301轉向,實現偽靜態
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+)\.html$ news.php?id=$1
將news.php?id=123這樣的地址轉向到news-123.html
最後:在使用301永久性重定向命令讓多個域名指向網站主域名時,也不會對網站的排名產生任何負面影響。希望對你有幫助。
thinkphp內核程序,無法重定向
ThinkPHP redirect 方法
ThinkPHP redirect 方法可以實現頁面的重定向(跳轉)功能。redirect 方法語法如下:
$this-redirect(string url, array params, int delay, string msg)
參數說明:
參數
說明
url 必須,重定向的 URL 表達式。
params 可選,其它URL參數。
delay 可選, 重定向延時,單位為秒。
msg 可選,重定向提示信息。
ThinkPHP redirect 實例
在 Index 模塊 index 方法中,重定向到本模塊的 select 操作:
class IndexAction extends Action{
public function index(){
$this-redirect(‘select’, array(‘status’=1), 3, ‘頁面跳轉中~’);
}
}
重定向後得到的 URL 可能為ex.php/Index/select/status/1
由於該方法調用了 U 函數來生成實際的 URL 重定向地址,因此重定向後的 URL 可能因配置不同而有所不同:
隱藏了入口文件 index.php 的
5idev.com/Index/select/status/1
隱藏了入口文件 index.php 且設置了偽靜態的
hom/Index/select/status/1.html
一些常用的 redirect 重定向例子:
// 不延時,直接重定向
$this-redirect(‘select’, array(‘status’=1));
// 延時跳轉,但不帶參數,輸出默認提示
$thi s-redirect(‘select’, ”, 3);
// 重定向到其他模塊操作
$this-redirect(‘Public/login’);
// 重定向到其他分組
$this-redirect(‘Admin-Public/login’);
提示: 1.當延時跳轉時,必須輸入 params 參數(可以為空),也就是 delay 必須出現在第 3 位上。
2.如果發現跳轉後的 URL 有問題,由於 redirect 方法調用 U 方法來生成跳轉後的地址,這時候可以測試一下 U 方法生成的地址是否正確,再檢查一下系統配置。
3.如果不想使用 U 方法生成跳轉地址,可以直接使用 PHP header 函數或 $this-redirect 的原型函數 redirect(string url, int delay, string msg),注意該 url 是個絕對地址,具體參見 PHP header 函數。
redirect 重定向與 success/error 跳轉的區別
•redirect 是使用的 PHP header 重定向,而 success/error 是使用的 html meta http-equiv=’Refresh’ 屬性跳轉。
•redirect 無模板頁面,輸出的提示信息是直接在函數內 echo 輸出的,而 success/error 有對應的模板。
•redirect 與 success/error 都可以實現頁面的跳轉,只是 redirect 可以無延時重定向,具體採用哪種視具體情況而定。
如何用 PHP 實現 302 重定向到其他 URL
302是臨時重定向的意思。表示被訪問頁面因為各種需要被臨時跳轉到其他頁面。
PHP里的302重定向非常簡單,只要在返回的HTTP Response Header里添加Location字段,PHP將自動返回302狀態碼。
例如:
?php
header(“Location: URL地址”);
?
這段代碼將自動重定向到URL地址
注意的是,跳轉不是在收到response header的時候馬上進行,也就是說頁面的剩餘內容會被下載來之後瀏覽器才會跳轉。新手常犯的一個錯誤是,在邏輯判斷時對符合條件的情況進行header跳轉之後,忘了在之後加上exit(),導致錯誤。例如,用user_login()判斷用戶是否進行了登錄,如果未登錄則跳轉到登錄頁面。代碼如下:
?php
if(!user_login()){
header(“Location:login.php”);
}
//display contents for login users.
?
這裡,容易以為header之後這段代碼就結束了,沒有在header之後使用exit()。後面的代碼繼續被執行,導致未登錄用戶看到了已登錄用戶才能看到的內容。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/192264.html