前几天双十一在阿里云买了一台ECS,顺便换了域名,也就是说,除了域名不同,其他完全一样。

因为习惯用Apache,所以通过.htaccess实现全站301到新的域名来。

到现在也过去了三天吧,无聊时候查询到360已经收录了首页,但是域名是这样的:

www.songhaifeng.com/index.php

我就纳闷了,后面怎么有index.php,好吧,第一反应就是301过来的。

检查一下301,才发现并没有实现我的功能。

因为无论打开原来站点的任何一个链接,跳转过来的都是上面那个带index.php的地址。

百度找了一翻,最后才发现问题出现在.htaccess规则上。

原来的规则是这样的:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.luckyhai.com [NC]
RewriteRule ^(.*)$ https://www.songhaifeng.com/$1 [L,R=301]
</IfModule>

所以上面的功能是:

访问http://blog.luckyhai.com/ 跳转到 https://www.songhaifeng.com/;

访问http://blog.luckyhai.com/后面再加任何东西,都会跳转到https://www.songhaifeng.com/,并且在后面加了个index.php(what???)

改动一下吧,变成这样:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog.luckyhai.com [NC]
RewriteRule ^(.*)$ https://www.songhaifeng.com/$1 [R=301,L]
</IfModule>

是的,没错。。仅仅是把R=301和L换个位置,然后现在的功能是:

访问http://blog.luckyhai.com/  跳转到 https://www.songhaifeng.com/;

访问http://blog.luckyhai.com/guestbook.html  跳转到 https://www.songhaifeng.com/guestbook.html;

访问http://blog.luckyhai.com/ZBlog/42.html  跳转到 https://www.songhaifeng.com/ZBlog/42.html;

OK,实现我想要的功能了。。收工。。

不太懂那规则的意思,所以。。这里作为记录,以免以后继续踩坑。。


2020.11.08新增以下两个方式:

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


RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R=301,L]