背景
虚拟主机即将到期且位置在香港,为了网站更好的访问速度索性就迁回大陆。在好多年前购买的时候只有Apache
可选,这次迁回有Nginx
可以选择,官方有说这个性能更加的强劲,所以这次就选了Nginx
服务。数据以及文件很容易就完成了迁移,本想万事大吉,随便点击了几下测试全是404
,其中包括文章、分类、标签、搜索、分页、站点地图、日期等全部404。WordPress
后台固定链接设置的是日期和名称,应该就是URL
重写的问题了。在使用Apache
的虚拟主机的时候是默认的有相关配置的,但是新购的Nginx虚拟主机就没有这种配置了。所以,只有自己来进行URL
的重写配置。
Nginx解决方法
Nginx
重写URL
配置内容如下:
location / {
if (!-e $request_filename) {
rewrite ^/([0-9]+?)/([0-9]+?)/([0-9]+?)/([^/]+)$ /index.php?year=$1&monthnum=$2&day=$3&name=$4 last;
rewrite ^/([0-9]+?)/([0-9]+?)/([0-9]+?)/?$ /index.php?year=$1&monthnum=$2&day=$3 last;
rewrite ^/([0-9]+)/([0-9]+)/?$ /index.php?year=$1&monthnum=$2 last;
rewrite ^/category/(.*)/page/([0-9]+?)/?$ /index.php?category_name=$1&paged=$2 last;
rewrite ^/category/(.*)$ /index.php?category_name=$1 last;
rewrite ^/tag/(.*)/page/([0-9]+?)/?$ /index.php?tag=$1&paged=$2 last;
rewrite ^/tag/(.*)/?$ /index.php?tag=$1 last;
rewrite ^/page/([0-9]+?)/?$ /index.php?paged=$1 last;
rewrite ^/([0-9]+?)/([0-9]+?)/page/([0-9]+?)/?$ /index.php?year=$1&monthnum=$2&paged=$3 last;
rewrite ^/search/(.+?)/page/([0-9]+?)/?$ /index.php?s=$1&paged=$2 last;
rewrite ^/search/(.+?)/?$ /index.php?s=$1 last;
rewrite ^/(.+?)$ /index.php?pagename=$1 last;
}
try_files $uri $uri/ /index.php?$args;
}
location ~* /feed/?$ {
try_files $uri /index.php?$args;
}
location = /wp-sitemap.xml {
try_files $uri $uri/ /index.php?$args;
}
location ~ ^/wp-sitemap(-index)?.xsl {
try_files $uri $uri/ /index.php?$args;
}
location ~ ^/wp-sitemap-(.*?)-(.*?).xml$ {
try_files $uri $uri/ /index.php?$args;
}
如果觉得以上配置太过臃肿,可以使用以下配置
location / {
try_files $uri $uri/ /index.php?$args;
}
Apache中解决方法
在.htaccess
文件中加入以下代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>