开启站点伪静态可以让展现在地址栏上的网址更具可读性,江湖传闻开启伪静态也更有利于 SEO,更容易让站点内容为搜索引擎所收录。在 LAMP 环境下,Wordpress 开启伪静态的同时会在根目录自动生成一个 .htaccess 文件(有权限的情况下),后续需要增加条件,可以直接修改该隐藏文件,当服务器为nginx时,就没有那么方便了,我们需要手动在配置文件中添加如下语句:
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
笔者的服务器采用了引入外部文件配置的方法,外部文件整体内容如下,已加入伪静态语句,一并贴上供参考:
server {
listen 80;
server_name www.biib.top ;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
listen 443 ssl;
server_name www.biib.top;
root /usr/share/nginx/html/wordpress;
if ($host != 'www.biib.top'){
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
ssl_certificate /etc/pki/CA/certs/biib.top.pem;
ssl_certificate_key /etc/pki/CA/certs/biib.top.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
Wordpress后台伪静态位置如下图所示,伪静态网址的格式众说纷纭,大方向上来看,应该是目录深度越浅越好,因为笔者文章数量不多,不容易出现因词汇量不够导致文章重名的情况(虽然词汇也比较贫瘠),就选择了使用文章postname的方式。