本站正式启用新域名(defcon.cn),旧数据已全部迁移。为了做好SEO优化,301重定向必不可缺少。更换域名后,如何完美解决将整站链接实现301重定向?
首先在网站根目录下新建一个.htaccess(Apache服务器才有,nginx配置略不同哦)并加入以下代码:
RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
以上代码的作用是将所有请求都交给index.php来处理,然后再打开根目录下得wp-blog-header.php,并在文件开头 <?php 之后加入以下代码:
if (strtolower($_SERVER['SERVER_NAME'])!='defcon.cn') { $URIRedirect=$_SERVER['REQUEST_URI']; if(strtolower($URIRedirect)=="/index.php") { $URIRedirect="/"; } header('HTTP/1.1 301 Moved Permanently'); header('Location:https://defcon.cn'.$URIRedirect); exit(); }