301和302重定向_重定向:301、302、307 | 301指南

301和302重定向

什么是重定向? (What is redirection?)

Redirection is a way of forwarding the user to the URL that is different from the one they initially clicked on. Down below some of the most common types of redirection are listed.

重定向是一种将用户转发到与其最初单击的URL不同的URL的方法。 下面列出了一些最常见的重定向类型。

how to do a 301 redirect

301永久移动 (301 Moved Permanently)

A 301 redirect is a permanent redirect which passes between 90-99% of link equity (ranking power) to the redirected page. 301 signals that the page has been moved to another URL and the old URL is outdated.

301重定向是一种永久重定向,它将链接资产的90-99%(排名能力)传递到重定向的页面。 301表示页面已移至另一个URL,旧的URL已过时。

找到302(HTTP 1.1)/临时移动(HTTP 1.0) (302 Found (HTTP 1.1) / Moved Temporarily (HTTP 1.0))

302 is a temporary redirect which passes 0% of link equity, and shouldn't be used in most cases.

302是一个临时重定向,传递的链接资产为0%,在大多数情况下不应该使用。

As of now, the internet runs on an HTTP protocol which dictates how URLs work. In two versions of this protocol, the HTTP response status code is different:

到目前为止,互联网运行在HTTP协议上,该协议规定了URL的工作方式。 在此协议的两个版本中,HTTP响应状态代码不同:

  • HTTP 1.0: 302 status code is «Moved Temporarily» — current page has been moved temporarily to another URL.

    HTTP 1.0:302状态代码为“暂时移动”-当前页面已临时移动到另一个URL。
  • HTTP 1.1: the description phrase has been changed to «Found» — current page has been found.

    HTTP 1.1:描述短语已更改为“找到”-已找到当前页面。

307临时移动(仅HTTP 1.1) (307 Moved Temporarily (HTTP 1.1 Only))

A 307 redirect is the HTTP 1.1 successor of the 302 redirect. While the major crawlers will treat it like a 302 in some cases, it is best to use a 301 for almost all cases. The exception to this is when content is really moved only temporarily (such as during maintenance) and the server has already been identified by the search engines as 1.1 compatible.

307重定向是302重定向的HTTP 1.1后续版本。 尽管主要爬虫在某些情况下会将其视为302,但最好在几乎所有情况下都使用301。 唯一的例外是当内容仅真正临时移动时(例如在维护期间),并且搜索引擎已将服务器标识为与1.1兼容。

Since it's essentially impossible to determine whether the search engines have identified a page as compatible, it is generally best to use a 302 redirect for content that has been moved temporarily.

由于基本上不可能确定搜索引擎是否已将页面识别为兼容页面,因此通常最好对临时移动的内容使用302重定向。

其他重定向类型 (Other redirection types)

There are also some other types of redirection: Meta Refresh or JavaScript redirection that are executed on the page level rather than the web server level. This is what a typical Meta Refresh redirect looks like:

重定向还有其他一些类型:元刷新或JavaScript重定向在页面级别而不是Web服务器级别执行。 这是典型的Meta Refresh重定向的样子:

<meta http-equiv="refresh" content="2;url=http://example.com/" />

It's best not to use these types of redirect, as they are often used by spammers and doorway pages. Besides, they pass little to none of the link juice.

最好不要使用这些重定向类型,因为垃圾邮件发送者和门禁页面经常使用它们。 此外,它们几乎没有通过任何链接

使用重定向的示例 (Examples of using redirects)

将您的域重定向到非www URL: (Redirecting your domain to a non-www URL:)

RewriteCond %{HTTP_HOST} ^www.site\.com$ [NC]
RewriteRule ^(.*)$ http://site.com/$1 [R=301,L]

将您的域重定向到www URL: (Redirecting your domain to a www URL:)

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

To choose which URL to make canonical, consider:

要选择要规范的URL,请考虑:

  • which URL ranks higher in the SERPs;

    在SERP中哪个URL排名更高;
  • which URL is more represented in the index.

    哪个网址在索引中更具代表性。

将您的域重定向到URL而不加斜杠 (Redirecting your domain to URL without a slash)

When developing a website, it's important to choose whether you want to add a slash to the links, because the search engines consider the links

开发网站时,选择是否要在链接中添加斜杠非常重要,因为搜索引擎会考虑链接

www.site.com/cat1

www.site.com/cat1

and

www.site.com/cat1

www.site.com/cat1

to be different. Then, you'll have to add the following code:

有所不同。 然后,您必须添加以下代码:

要从网址中删除斜杠: (To delete the slash from the URLs:)

RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /$ [NC]
RewriteRule ^(.*)(/)$ $1 [L,R=301]

要将斜杠添加到URL: (To add the slash to the URLs:)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*[^/])$ $1/ [L,R=301]

要将用户从一个页面重定向到另一页面: (To redirect the user from one page to another:)

Redirect 301 /oldpage.html http://www.site.com/newpage.html

重定向主页重复项 (Redirecting the main page duplicates)

This code ensures that if there are multiple versions of the direct link to the main page (index, etc.), they will all redirect to the canonical main page:

这段代码确保了如果存在指向主页的直接链接的多个版本(索引等),它们都将重定向到规范的主页:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index|main)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.site.com/$1 [L,R=301]

重定向目录 (Redirecting catalogs)

If the URLs reflect the structure of a catalog, changes in the catalog will lead to changes in the URLs. In this case, use the following redirect:

如果URL反映目录的结构,则目录中的更改将导致URL的更改。 在这种情况下,请使用以下重定向:

RewriteRule ^(.*)/old-catalog/(.*)$ $1/new-catalog/$2 [R=301,L]

But, if the URL of the previous catalog comes right after the name of a domain: www.site.com/old-catalog, use this code:

但是,如果先前目录的URL在域名之后: www.site.com/old-catalog ,请使用以下代码:

RewriteRule old-catalog /(.*) / old-catalog /$1 [R=301,L]

If you've switched platforms or a CMS and only the URLs' extension has changed, use this redirect:

如果您已切换平台或CMS,但仅URL的扩展名已更改,请使用以下重定向:

RedirectMatch 301 (.*)\.php$ http://www.site.com$1.html

Examples of using redirection to avoid duplicate pages

使用重定向避免重复页面的示例

重定向(子)域 (Redirecting (sub-)domains)

In case you've bought several domains with multiple TLDs, OR used a subdomain to develop a new website and forgot to block it from being indexed by the search engines. Either way, you have to set up redirection to the root domain:

如果您购买了具有多个TLD的多个域,或者使用了子域来开发新网站,却忘记了阻止它被搜索引擎索引。 无论哪种方式,您都必须设置重定向到根域:

RewriteCond %{HTTP_HOST} !^www\.site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

That way, all the (sub-)domains like www.site.org, www.site.net, test.site.com, will redirect to www.site.com

这样,所有(子)域(如www.site.orgwww.site.net ,test.site.com)都将重定向到www.site.com

从网址中删除多个斜杠/连字符 (Deleting multiple slashes/hyphens from the URLs)

Sometimes, user can accidentally type in multiple slashes, e.g. www.site.com/catalogpage-1.html. In this case, you have to set up a 301 redirect to a page with a single slash www.site.com/catalog/page-1.html:

有时,用户可能会不小心输入多个斜杠,例如www.site.com/catalog/page-1.html 。 在这种情况下,您必须将301重定向设置为带有一个斜杠的页面www.site.com/catalog/page-1.html

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

In the same way, you can set up a redirect from a URL with multiple hyphens (for example, www.site.com/catalog/page---1.html) to www.site.com/catalog/page-1.html:

以相同的方式,您可以设置从具有多个连字符的URL(例如, www.site.com/catalog/page---1.html )到www.site.com/catalog/page-1的重定向。 html

RewriteCond %{REQUEST_URI} ^(.*)--(.*)$
RewriteRule . %1-%2 [R=301,L]

从任何URL重定向到小写URL (Redirecting from any URL to a lowercase URL)

The search engines notice the letter case, so it's best to have your URLs in lowercase. If you haven't developed your website with this in mind, you can use this PHP-script:

搜索引擎会注意到字母的大小写,因此最好将您的URL小写。 如果您没有考虑到这一点来开发网站,则可以使用以下PHP脚本:

$lowerURI=strtolower($_SERVER['REQUEST_URI']);
if($_SERVER['REQUEST_URI']!=$lowerURI)
{
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://" . $_SERVER['HTTP_HOST'] . $lowerURI);
exit();
}

How to move your website to a new domain? The optimal strategy for a 301 redirect

如何将您的网站移至新域? 301重定向的最佳策略

According to the most popular search engines, the best strategy for moving to a new domain is:

根据最受欢迎的搜索引擎,转移到新域的最佳策略是:

  • mounting a page-by-page 301 redirect from the old site to the new one;

    安装从旧站点到新站点的逐页301重定向;
  • don't set up a redirect from robots.txt — add the Host directive to the new domain instead.

    不要设置从robots.txt的重定向,而是将Host指令添加到新域中。

In this case, the code on the former website will have to look something like this:

在这种情况下,以前网站上的代码必须看起来像这样:

RewriteCond %{REQUEST_FILENAME} robots.txt$ [NC]
RewriteRule ^([^/]+) $1 [L]
RewriteCond %{HTTP_HOST} !^www\.site\.com
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
а файл robots.txt для старого сайта:
User-agent: Yandex
Disallow:
Host: newsite.com

产生301重新导向 (Generating 301 redirects)

If you're not particularly tech-savvy, you can use the online services for generating basic redirects:

如果您不是特别精通技术,则可以使用在线服务来生成基本重定向:

http://www.webconfs.com/htaccess-redirect-generator.php http://www.webconfs.com/htaccess-redirect-generator.php http://www.rapidtables.com/web/tools/redirect-generator.htm http://www.rapidtables.com/web/tools/redirect-generator.htm

Just enter your data and get a code for redirection between domains, directories, and URLs.

只需输入您的数据并获取用于域,目录和URL之间重定向的代码。

如何测试301重定向 (How to test the 301 redirect)

With every change in 301 redirect, you need to test the site's performance:

301重定向中的每一项更改都需要测试网站的性能:

  • whether it's working (check the main page);

    是否正常(请检查主页);
  • go through the main sections and webpages of the site.

    浏览网站的主要部分和网页。

301重定向VS Canonical-何时使用哪个? (301 redirect VS Canonical — which one to use & when?)

Minor details aside, in order to understand what exactly do we want to say, Google offers some clear-cut rules. In very simple terms, this is how the search engines understand our commands:

除了一些小细节,为了理解我们到底想说什么,Google提供了一些明确的规则。 简单来说,这就是搜索引擎理解我们命令的方式:

301 (301)

: okay, google (or any other search engine), my page isn't there anymore and it's been permanently moved to a new URL. Please, delete the old link from the index, and pass the link juice to the new page.

:好的,谷歌(或任何其他搜索引擎),我的页面已经不存在了,并且已经永久地移到了新的URL。 请从索引中删除旧链接,然后将链接汁传递到新页面。

典范 (Canonical)

: okay, google, I've got multiple versions of the same page (or its content), so, please, index only the canonical version. I will keep the other versions for people to see, but don't index them, please, and pass the link juice to the canonical page.

:好的,谷歌,我在同一页面上有多个版本(或其内容),因此,请仅对规范版本进行索引。 我将保留其他版本供人们查看,但请不要为它们建立索引,并将链接源传递到规范页面。

什么时候使用301重定向更好? (When is it better to use a 301 redirect?)

  • This is a preferred method by default;

    默认情况下,这是首选方法。
  • For pages, that have beenmoved permanently, or their URLs have been changed;

    对于已永久移动的页面或它们的URL已更改的页面;
  • For domains, if you've moved your website to the new domain;

    对于域,如果您已将网站移至新域;
  • For 404 pages. For example, if a certain product has been deleted from the catalog, you can set up a redirect to a page with a similar product, or to the URL of the product category.

    404页。 例如,如果某个商品已从目录中删除,则可以设置重定向到包含类似商品的页面或商品类别的URL。

什么时候使用rel =«canonical»更好? (When is it better to use rel= «canonical»?)

  • If you can't set up 301 redirects or it won't be time-efficient;

    如果您无法设置301重定向,否则将不会节省时间;
  • For duplicate content, if you want to keep both versions (for example, pages with different clothing sizes);

    对于重复的内容,如果要保留两个版本(例如,具有不同衣服尺寸的页面);
  • When you have multiple URLs leading to the same page (catalog categories, pages for tracking the traffic from affiliate links, etc.);

    当您有多个URL指向同一页面(目录类别,用于跟踪会员链接流量的页面等)时;
  • For cross-domain resource sharing, if you want to transfer data between pages that have different origins (domains, for example).

    对于跨域资源共享,如果要在具有不同来源(例如域)的页面之间传输数据。

把它们加起来 (To sum it up)

Both solutions pass the link juice and both are ranked by Google equally. 301 redirect is a bit more preferred, though.

两种解决方案都通过了链接,并且Google均对它们进行了排名。 不过,301重定向更为可取。

重定向错误 (Redirection mistakes)

  • Redirection chains (avoid them to maximize the speed and the link juice flow of the website);

    重定向链(避免使它们最大化网站的速度和链接流);
  • Using the wrong type of redirect (to make the right decision, you have to consider all the details);

    使用错误的重定向类型(做出正确的决定,您必须考虑所有细节);
  • Setting up internal redirects without rewriting the URLs of the links. You have to make sure that links on your website don't lead to pages with the redirect;

    设置内部重定向,而无需重写链接的URL。 您必须确保您网站上的链接不会导致带有重定向的页面;
  • Redirecting to irrelevant pages/content. Your links should always lead either to similar pages or to the section of the site that included the requested page;

    重定向到不相关的页面/内容。 您的链接应始终指向相似的页面,或指向包含所请求页面的网站部分;
  • The wrong choice of either rel=canonical or 301 direct (see above);

    rel = canonical或301 direct的错误选择(请参见上文);
  • Redirecting robots.txt (it's better to add the Host directive);

    重定向robots.txt(最好添加Host指令);
  • Any redirect, which doesn't lead to a page with a 200 status code (every link should lead to a properly working page with a 200 status response. Otherwise, don't confuse the crawlers and just show the 404 Error page).

    任何重定向都不会导致状态码为200的页面(每个链接都应导致状态响应为200的正常工作页面。否则,请不要混淆搜寻器,而仅显示404错误页面)。

Hopefully, this 301 how-to guide will be your cheat sheet and will help you to use a 301 redirect on your website.

希望这份301入门指南能成为您的备忘单,并帮助您在网站上使用301重定向。

If you have any questions, ask them down below. I will try my best to help you!

如果您有任何疑问,请在下面提问。 我会尽力为您服务!

Read also:

另请阅读:

Essential on-site SEO factors. 必要的现场SEO因素

翻译自: https://habr.com/en/post/458108/

301和302重定向

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值