PHP 标头重定向 301 - 有什么影响?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7324645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:29:40  来源:igfitidea点击:

PHP header redirect 301 - what are the implications?

phpredirectheaderhttp-status-code-301

提问by Andres SK

I have domain.com. If the user is logged in, it should load automatically domain.com/option-Xwhere X is a predefined choice of the user.

我有domain.com。如果用户已登录,则应自动加载domain.com/option-X,其中 X 是用户的预定义选项。

So, I do this at the top of index.php:

所以,我在 index.php 的顶部这样做:

header("Location: /option-X"); 

But, if the user is not logged in, I just choose automatically the first option like this:

但是,如果用户没有登录,我会自动选择第一个选项,如下所示:

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: /option-a"); 

So, i have two questions regarding the implications of doing so:

所以,我有两个关于这样做的影响的问题:

  1. Since the search engines crawlers won't be logged in, they will always get domain.com/option-a- does it affect them that it has a 301 header?
  2. What could be the server cpu load of doing those redirects? I don't know how to make a test out of it. The current site (which has no redirects) has about 100k daily visits.
  1. 由于搜索引擎爬虫不会登录,他们总是会得到domain.com/option-a- 它有一个 301 标头会影响他们吗?
  2. 执行这些重定向的服务器 CPU 负载可能是多少?我不知道如何进行测试。当前站点(没有重定向)每天有大约 10 万次访问。

回答by Roel Veldhuizen

The effect of the 301 would be that the search engines will index /option-a instead of /option-x. Which is probably a good thing since /option-x is not reachable for the search index and thus could have a positive effect on the index. Only if you use this wisely ;-)

301 的效果是搜索引擎将索引 /option-a 而不是 /option-x。这可能是一件好事,因为搜索索引无法访问 /option-x,因此可能对索引产生积极影响。仅当您明智地使用它时;-)

After the redirect put exit(); to stop the rest of the script to execute

重定向后 put exit(); 停止执行脚本的其余部分

header("HTTP/1.1 301 Moved Permanently"); 
header("Location: /option-a"); 
exit();

回答by Gary Samad

This is better:

这个更好:

<?php
//* Permanently redirect page
header("Location: new_page.php",TRUE,301);
?>

Just one call including code 301. Also notice the relative path to the file in the same directory (not "/dir/dir/new_page.php", etc.), which all modern browsers seem to support.

只调用一次,包括代码 301。还要注意同一目录中文件的相对路径(不是“/dir/dir/new_page.php”等),所有现代浏览器似乎都支持。

I think this is valid since PHP 5.1.2, possibly earlier.

我认为这自 PHP 5.1.2 起就有效,可能更早。

回答by the_nuts

Just a tip: using http_response_codeis much easier to remember than writing the full header:

只是一个提示:使用http_response_code比编写完整的标头更容易记住:

http_response_code(301);
header('Location: /option-a'); 
exit;

回答by genesis

Make sure you die()after your redirection, and make sure you do your redirect AS SOON AS POSSIBLE while your script executes. It makes sure that no more database queries (if some) are not wasted for nothing. That's the one tip I can give you

确保die()在重定向之后进行,并确保在脚本执行时尽快进行重定向。它确保没有更多的数据库查询(如果有的话)不会白白浪费。这是我能给你的一个提示

For search engines, 301 is the best response code

对于搜索引擎,301是最好的响应码

回答by profitphp

Search engines like 301 redirects better than a 404 or some other type of client side redirect, no worries there.

像 301 重定向这样的搜索引擎比 404 或其他类型的客户端重定向更好,不用担心。

CPU usage will be minimal, if you want to save even more cycles you could try and handle the redirect in apache using htaccess, then php won't even have to get involved. If you want to load test a server, you can use ab which comes with apache, or httperf if you are looking for a more robust testing tool.

CPU 使用率将最小,如果您想节省更多周期,您可以尝试使用 htaccess 在 apache 中处理重定向,那么 php 甚至不必参与。如果你想对服务器进行负载测试,你可以使用 apache 自带的 ab,如果你正在寻找更强大的测试工具,你可以使用 httperf。