好久没有关心博客了,今天发现用的还是这种带问号的地址(http://niliu.me/?p=200)。之前也就是知道这种地址对SEO不友好,今天查了一些资料对比了下,所谓的不友好是指搜索引擎会对这种网址降权,因为这种地址参数变(参数可以随便加),而内容不变。
我们当然希望我们的文章可以被更多的人通过搜索引擎查询到,那就需要重写这个地址,一般使用WEB服务器提供的rewrite模块就可以了(比如Nginx的ngx_http_rewrite_module),通过修改配置文件,增加规则,重新映射地址, Nginx的rewrite语法如下:
1 2 3 | Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if |
定义这样的规则后我们就可以达到以下目的:访问地址http://niliu.me/articles/263.html后,Nginx收到请求后自动映射到http://niliu.me/?p=263,而搜索引擎看到的是一个静态页面地址,就不会降权处理了,而该页面文件实际在服务器上不存在,这就是所谓的“伪静态”
Nginx下wp伪静态第一种配置方式
1 2 3 4 5 6 7 8 9 10 11 12 13 | location / { if (-f $request_filename /index.html){ rewrite (.*) $1 /index.html break ; } if (-f $request_filename /index.php){ rewrite (.*) $1 /index.php; } if (!-f $request_filename ){ rewrite (.*) /index.php; } } |
完整的配置参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | server { listen 80; server_name niliu.me; root / var /www/html/niliu; index index.html index.htm index.php; location / { if (-f $request_filename /index.html){ rewrite (.*) $1 /index.html break ; } if (-f $request_filename /index.php){ rewrite (.*) $1 /index.php; } if (!-f $request_filename ){ rewrite (.*) /index.php; } } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME / var /www/html/niliu $fastcgi_script_name ; include fastcgi_params; } } |
一开始的时候location下面是这么写的, 根本解释不了那个s, 对if语句中的判断不清晰,可以参考Nginx中文文档
1 2 3 4 | if (!-e $request_filename ) { rewrite ^(.*)$ /index.php?s= $1 last; break ; } |
Nginx下wp伪静态第二种配置方式
1 2 3 | location / { try_files $uri $uri / /index.php? $args ; } |
完整配置参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | server { listen 80; server_name niliu.me; root / var /www/html/niliu; index index.html index.htm index.php; location / { try_files $uri $uri / /index.php? $args ; } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME / var /www/html/niliu $fastcgi_script_name ; include fastcgi_params; } } |
PS:
0、如果只是使用带问号的链接,根本不需要rewrite, 上面的两个location都不需求,那么wp中index.php是如何处理的
1、在wp后台设置->固链接接->自定义结构->填写’http://niliu.me/articles/%post_i既%.html’即可
参考:
nginx rewrite
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if
http://www.nginx.cn/nginx-how-to
伪静态
https://www.zhihu.com/question/20153311
https://xuexb.com/post/nginx-url-rewrite.html
wordpress 配置
https://www.nginx.com/resources/wiki/start/topics/recipes/wordpress/