T086ѧϰ�� | վ��ѧԺ | �����ĵ� | ���� | Ъ���� | IP��ַ��ѯ | �������� | ���ջ��� | �����ֵ� | ���ù��� | ������Ϣ��ѯ

ʹ��PHP����HTTP�ض���(301,302)

�� һ��Blog ����ʱ�䣺2010-10-28 | ���壺�� �� С��
[����]��Google���ѡ�PHP �ض��򡱣���ƪ��־�������ڵ��ġ�������ƪд��ʵ��̫���ˡ���дһƪ�� ʲô��HTTP�ض����� ��������www.yegle.netʱ����ַ���ĵ�ַ���Զ�����yegle.net����������ye.gl��ʱ...

��Google����“PHP �ض���”����ƪ��־�������ڵ��ġ�������ƪд��ʵ��̫����…��дһƪ…

ʲô��HTTP�ض�����

��������www.yegle.netʱ����ַ���ĵ�ַ���Զ�����yegle.net����������ye.gl��ʱ�򣬵�ַ���ĵ�ַ������yegle.net��������HTTP�ض���

һ��HTTP���󣬷��ص�HTTP Response Header���һ����HTTP��״̬�롣���������£�HTTP���󷵻ص�״̬����200 OK��
������HTTP���󣬷���200 OK��

< HTTP/1.1 200 OK
< Date: Fri, 16 Jul 2010 11:21:10 GMT
< Server: Apache
< X-Powered-By: PHP/5.2.4-2ubuntu5.10
< Set-Cookie: PHPSESSID=7f86ed2e5a4750275e98971773ac88ab; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Set-Cookie: wassup=ZjVjMWExMjZjNmIxNzU1NDBhZjU0MmM5MzhmYjllZDQ6OjEyNzkyODE5NzE6Ojo6MjAwMTozODg6ZjAwMDo6ZTZmOjoyMDAxOjM4ODpmMDAwOjplNmY%253D; expires=Fri, 16-Jul-2010 12:11:11 GMT; path=/
< X-Pingback: http://yegle.net/xmlrpc.php
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
<

�����ıȽ�֪���Ļ������磺

404 Not Found
301 Moved Permanently
302 Found
500 Internal Server Error
HTTP�ض�������ͨ��301��302����״̬����ʵ�ֵġ�

302����ʱ�ض�������˼����ʾ������ҳ����Ϊ������Ҫ����ʱ��ת������ҳ�档�����������Ƿ��� http://yegle.net/recursion.php

< HTTP/1.1 302 Found
< Date: Fri, 16 Jul 2010 11:26:48 GMT
< Server: Apache
< X-Powered-By: PHP/5.2.4-2ubuntu5.10
< Location: http://yegle.net/recursion.php
< Cache-Control: max-age=600
< Expires: Fri, 16 Jul 2010 11:36:48 GMT
< Vary: Accept-Encoding
< Content-Length: 0
< Content-Type: text/html
<

���������յ�302 Found��״̬��֮�����ڷ��ص�HTTP Response Header�в���Location�ֶΣ�Ȼ�����ʶ�Ӧ��ַ�������������У��������ͻ����� http://yegle.net/recursion.php (������һ���ݹ飬�㶮��…)

301�������ض��������������Ӻܺ��ҡ����� http://google.com

< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Fri, 16 Jul 2010 11:29:07 GMT
< Expires: Sun, 15 Aug 2010 11:29:07 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 1; mode=block
<

ͬ�����������ڷ���301��״̬��֮��������Location�ֶΣ�Ȼ�������Ǹ���ַ��

Location�ֶεĸ�ʽ�����⣬�ȿ����Ǿ��Ե�ַ��Ҳ���������Ե�ַ�������������Ը�Ŀ¼�ĵ�ַ������Location�ֶζ��ǺϷ��ģ�

Location: http://yegle.net/
Location: /test/index.php
Location: index.php
Location: ../index.php

301�ض�����302�ض�����SEO�Լ����������зdz��������ġ�

����SEOҲ�������������Ż���һ��ҳ��302�ض�������һ��ҳ�棬��ҳ����PageRank������ԭҳ��Ӱ�졣��һ��ҳ��301�ض�������һ��ҳ�棬ԭҳ����PageRank�ᱻ���ݵ���ҳ�档���Զ���һ����վ��������ת�������õķ�������ʹ��301�ض������ھ���һ��ʱ��֮�����Բ���ʧPR�ؽ�ȫվת�Ƶ��������¡�

����HTTP��������������squid��˵������һ��ҳ����302�ض�������ҳ�沢��û��ָ��Expire HTTPͷ��squid��������������Ϣ��Ҳ����˵ÿ���û�ͨ����������ʱ�������»�ȡһ�顣������301�ض�����squid���Խ����������Ա�������Ӧ��һ��������ͬҳ�����û���

PHP����302�ض����dz��򵥣�ֻҪ�ڷ��ص�HTTP Response Header������Location�ֶΣ�PHP���Զ�����302״̬�롣���磺

< ?php
header("Location: http://yegle.net/recursion.php");
?>
���δ��뽫�Զ��ض�����http://yegle.net/recursion.php

��301�ض�������΢�е㸴�ӣ���Ҫֱ�ӽ�301״̬����header�������ظ��û������磺

< ?php
    header( "HTTP/1.1 301 Moved Permanently" );
    header( "Location: http://yegle.net/" );
?>
ע�����ǣ���ת�������յ�response header��ʱ�����Ͻ��У�Ҳ����˵ҳ����ʣ�����ݻᱻ������֮���������Ż���ת�����ֳ�����һ�������ǣ����߼��ж�ʱ�Է�����������������header��ת֮����������֮������exit()�����´��������磬��user_login()�ж��û��Ƿ������˵�¼������δ��¼����ת����¼ҳ�档�������£�

if(!user_login()){
    header("Location:login.php");
}
//display contents for login users.
���������Ϊheader֮�����δ����ͽ����ˣ�û����header֮��ʹ��exit()�������Ĵ���������ִ�У�����δ��¼�û��������ѵ�¼�û����ܿ��������ݡ�

ԭ�ģ�http://yegle.net/2010/07/16/http-redirect-using-php/

  • ת����ע����Դ��ITѧϰ�� ��ַ��http://www.t086.com/ �����������Ƽ�������
  • �ر������� ��վ�������ر�������ֹת�ص�ר�������������¿�������ת�أ���������ע��������ԭʼ���ߡ����°�Ȩ������ԭʼ�������С����ڱ���վת�����µĸ��˺���վ�����DZ�ʾ������л�⡣������վת�ص������а�Ȩ��������ϵ���ǣ����ǻᾡ�����Ը�����
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
  • ��
������ѯ Health Check Gulangyu
© 2017 T086ѧϰ�� - T086.com(ԭitlearner.com)
RunTime:7.07ms QueryTime:7