PHP重定向到另一个URL /网页脚本示例
时间:2020-01-09 10:43:00 来源:igfitidea点击:
如何使用PHP脚本重定向?
如何使用PHP脚本将用户从他们输入的URL重定向到其他网页/URL?
在PHP下,您需要使用header()
发送原始HTTP标头。
使用" headers()"方法,您可以轻松地转移到新页面,而无需单击链接以继续。
这对于搜索引擎也很有用。
请记住,在发送任何实际输出之前,必须通过常规HTML标记,文件中的空白行或者从PHP调用header()。
使用include()或者require(),函数或者另一个文件访问函数读取代码,并在调用header()之前输出空格或者空行是一个非常常见的错误。
使用单个PHP/HTML文件时,存在相同的问题。
如何使用PHP重定向
您可以使用以下header(" Location:....)
语法轻松地进行重定向:
<?php /* Redirect browser */ header("Location: http://theitroad.com/"); /* Make sure that code below does not get executed when we redirect. */ exit; ?>
header()用于发送原始的HTTP/1.1规范特定的头。
必须在发送任何实际输出之前调用header(),以下示例将不起作用:
<?php $var="something"; echo "Hello world"; echo $var; /**************************************************** * Remember that header() must be called before any actual output is sent, * either by normal HTML tags, blank lines in a file, or from PHP. *****************************************************/ header("Location: http://theitroad.com/"); exit; ?>
PHP重定向与HTTP状态代码
在sitefunctions.php中创建一个名为movePage()的示例函数(请注意,我不是以下内容的作者,我在Internet上的其他地方找到了它):
function movePage($num,$url){ static $http = array ( 100 - "HTTP/1.1 100 Continue", 101 - "HTTP/1.1 101 Switching Protocols", 200 - "HTTP/1.1 200 OK", 201 - "HTTP/1.1 201 Created", 202 - "HTTP/1.1 202 Accepted", 203 - "HTTP/1.1 203 Non-Authoritative Information", 204 - "HTTP/1.1 204 No Content", 205 - "HTTP/1.1 205 Reset Content", 206 - "HTTP/1.1 206 Partial Content", 300 - "HTTP/1.1 300 Multiple Choices", 301 - "HTTP/1.1 301 Moved Permanently", 302 - "HTTP/1.1 302 Found", 303 - "HTTP/1.1 303 See Other", 304 - "HTTP/1.1 304 Not Modified", 305 - "HTTP/1.1 305 Use Proxy", 307 - "HTTP/1.1 307 Temporary Redirect", 400 - "HTTP/1.1 400 Bad Request", 401 - "HTTP/1.1 401 Unauthorized", 402 - "HTTP/1.1 402 Payment Required", 403 - "HTTP/1.1 403 Forbidden", 404 - "HTTP/1.1 404 Not Found", 405 - "HTTP/1.1 405 Method Not Allowed", 406 - "HTTP/1.1 406 Not Acceptable", 407 - "HTTP/1.1 407 Proxy Authentication Required", 408 - "HTTP/1.1 408 Request Time-out", 409 - "HTTP/1.1 409 Conflict", 410 - "HTTP/1.1 410 Gone", 411 - "HTTP/1.1 411 Length Required", 412 - "HTTP/1.1 412 Precondition Failed", 413 - "HTTP/1.1 413 Request Entity Too Large", 414 - "HTTP/1.1 414 Request-URI Too Large", 415 - "HTTP/1.1 415 Unsupported Media Type", 416 - "HTTP/1.1 416 Requested range not satisfiable", 417 - "HTTP/1.1 417 Expectation Failed", 500 - "HTTP/1.1 500 Internal Server Error", 501 - "HTTP/1.1 501 Not Implemented", 502 - "HTTP/1.1 502 Bad Gateway", 503 - "HTTP/1.1 503 Service Unavailable", 504 - "HTTP/1.1 504 Gateway Time-out" ); header($http[$num]); header ("Location: $url"); } }
首先包含sitefunctions.php,然后按如下所示调用movePage():
<?php @include("/path/to/sitefunctions.php"); /* Move page with 301 http status code*/ movePage(301,"http://www.theitroad.local/"); ?>
如何测试网址重定向?
如果您使用的是Apple OS X或者类似Linux/Unix的操作系统,请打开"终端"应用程序,然后执行curl命令,如下所示:
$ curl -I your-url-here $ curl -I www.theitroad.local/tips/
输出示例:
HTTP/1.1 301 Moved Permanently Server: nginx Date: Mon, 30 Dec 2013 22:31:37 GMT Connection: keep-alive Location: http://www.theitroad.local/ X-Galaxy: Andromeda-2
如果您在MS-Windows上或者不想使用命令行,请尝试使用redbot工具:
使用redbot工具检查HTTP位置url重定向