301 转跳

这是最彻底的方法来重定向一个URL。快速,简便,搜索引擎友好。记住 htaccess 的是Apache服务器专用的。

# 单页转跳

Redirect 301 /oldpage.html http://www.example.com/newpage.html
Redirect 301 /oldpage2.html http://www.example.com/folder/

# 转跳到新的网址

例如:oldsite.com/abc/ 会转跳到新的 newsite.com/abc/
换域名时,把这行 htaccess 代码放到旧网站的根目录

Redirect 301 / http://newsite.com/

# www 或者 https

访问 http://example.com
转跳到 http://www.example.com

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

访问 http://example.com
转跳到 https://www.example.com

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

# /folder1/ 转到 /folder2/

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1/(.*)$ http://example.com/folder2/$1 [R=301,L]

# /abc.html 转到 /folder1/abc.html

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteCond %{HTTP_HOST} !folder1
RewriteRule ^(.*)$ http://example.com/folder1/$1 [R=301,L]