简体   繁体   English

将 http 重定向到 https 和 301 重定向

[英]Redirect http to https and 301 redirect

I have around 500 links that the SEO person is requesting be updated after a site refresh.我有大约 500 个链接,SEO 人员要求在网站刷新后更新这些链接。 The main change is the use of https for everything, instead of http. I want to make the.htaccess file as simple as possible.主要变化是对所有内容使用 https,而不是 http。我想让 .htaccess 文件尽可能简单。 The links fall into the following 4 groups.这些链接分为以下 4 组。

Group One (site root)第一组(站点根目录)

http://www.example.com -> https://www.example.com http://www.example.com -> https://www.example.com

http://example.com -> https://www.example.com http://example.com //示例.com -> https://www.example.com .示例.com

Group Two (http to https)第二组(http 到 https)

http://www.example.com/samepage -> https://www.example.com/samepage http://www.example.com/samepage -> https://www.example.com/samepage

Group Three (lots of old pages and directories forwarding to a single new page or directory)第三组(许多旧页面和目录转发到一个新页面或目录)

http://www.example.com/old -> https://www.example.com/new http://www.example.com/old -> https://www.example.com/new

http://www.example.com/aged -> https://www.example.com/new http://www.example.com/aged -> https://www.example.com/new

Group Four (old page to new page)第四组(旧页到新页)

http://www.example.com/oldpage -> https://www.example.com/newpage http://www.example.com/oldpage -> https://www.example.com/newpage

I've looked at 301 redirects but they don't use the full old URL to new, just the subdirectory part.我查看了 301 重定向,但它们没有使用完整的旧 URL 到新的,只是子目录部分。 Do I need to change HTTP to HTTPS before or after the 301's?我需要在 301 之前或之后将 HTTP 更改为 HTTPS 吗?

These mod_rewrite directives should go near the top of your .htaccess file.这些 mod_rewrite 指令应该 go 靠近.htaccess文件的顶部。 The most specific redirects should go first (ie. Groups three and four), followed by groups One and Two.最具体的重定向应该首先是 go(即第三组和第四组),然后是第一组和第二组。 (Groups One and Two are really the same thing.) (第一组和第二组实际上是同一回事。)

Groups Three and Four are standard redirects old URL to new URL, regardless of scheme/hostname requested.第三组和第四组是标准重定向旧 URL 到新 URL,无论请求的方案/主机名如何。

RewriteEngine On

# Specific redirects....
# Groups Three and Four - whichever is more specific should come first
RewriteRule ^oldpage$ https://www.example.com/newpage [R,L]

RewriteRule ^(old|aged)$ https://www.example.com/new [R,L]

# Canonical redirect (HTTP to HTTPS and apex to www)
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.example.com/$1 [R,L]

By having the directives in this order you should only ever get at most 1 redirect.通过按此顺序使用指令,您最多只能获得 1 次重定向。 You could instead include the canonical redirect first, then you wouldn't need to specify an absolute URL in the other redirects.您可以先包含规范重定向,然后您就不需要在其他重定向中指定绝对 URL。 However, this would potentially result in an additional redirect when accessing one of the old URLs.但是,这可能会在访问其中一个旧 URL 时导致额外的重定向。

These are currently temporary (302) redirects.这些目前是临时 (302) 重定向。 Change R to R=301 (permanent) only when you are sure it's working OK.只有当您确定它工作正常时,才将R更改为R=301 (永久)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM