简体   繁体   中英

Redirect http to https and 301 redirect

I have around 500 links that the SEO person is requesting be updated after a site refresh. The main change is the use of https for everything, instead of http. I want to make the.htaccess file as simple as possible. The links fall into the following 4 groups.

Group One (site root)

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

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

Group Two (http to https)

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/aged -> https://www.example.com/new

Group Four (old page to new page)

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. Do I need to change HTTP to HTTPS before or after the 301's?

These mod_rewrite directives should go near the top of your .htaccess file. The most specific redirects should go first (ie. Groups three and four), followed by groups One and Two. (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.

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. You could instead include the canonical redirect first, then you wouldn't need to specify an absolute URL in the other redirects. However, this would potentially result in an additional redirect when accessing one of the old URLs.

These are currently temporary (302) redirects. Change R to R=301 (permanent) only when you are sure it's working OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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