nginx设置Wordpress自定义固定链接后的地址转发

无简介

在使用nginx之前,为了实现伪静态,我在wordpress后台设置里面的固定链接中,将“常用设置”设置为了固定链接,并在里面默认值(/archives/%post_id%)的基础上在后面添加了“.html” 后来使用了nginx之后,突然又发现.html不能正常打开了,显示的是nginx的404,没多想着急忙慌的就在固定链接里面选择了“数字型”,倒是没问题了 可是我之前的外链怎么办啊? 以前在其他地方留文章的外链留下的可都是带html的,总不能放弃这些文章吧 于是就在nginx里面配置了一下转发规则,将符合规则的.html路径对应到数字型的路径下,就可以正常访问了。 下面是两个我配置过的访问规则,我自己试了试,是可以的:

   if ($request\_filename ~ (.\*)/archives/(.\*)\\.html$){
      rewrite (/archives/.\*)\\.html $1 permanent;
   }    

这一条,适合于后缀为".html"的转成去掉html的,如“https://sumile.cn/archives/1688.html”转成“https://sumile.cn/archives/1688”

   if ($request\_filename ~ (.\*)/archives/\[0-9\]\*$){
       rewrite (/archives/\[0-9\]\*$) $1.html permanent;
   }    

而这一条,适合给原来的链接添加后缀html的,如“https://sumile.cn/archives/1688” 转成 “https://sumile.cn/archives/1688.html” 对于以上两条,如果你的url中不是如我一样的“/archives/”的话,那么将这个修改为你自己的部分 另外,你要转成什么样子的,就在 固定链接 里面的常用设置那里选择什么样子的:如我最后要转成"https://sumile.cn/archives/1688.html",那么我在这里就应该选择 自定义链接,然后里面要填写 “/archives/%post_id%.html” 。如果我要转成"https://sumile.cn/archives/1688”,那么我选择“数字型”或者在 “自定义链接” 里面填写 “/archives/%post_id%”就可以了 各位根据自己的需要自己修改就好了。 下面我再发一个我完整的配置

server {
listen 80;
server_name sumile.cn www.sumile.cn ;
autoindex off;
index index.php;

location / {
    root   C:\\wamp\\www\\blog;
    index  index.php;
   #if ($request\_filename ~ (.\*)/archives/(.\*)\\.html$){
   #   rewrite (/archives/.\*)\\.html $1 permanent;
   #}    
   if ($request\_filename ~ (.\*)/archives/\[0-9\]\*$){
       rewrite (/archives/\[0-9\]\*$) $1.html permanent;
   }           
    if (-f $request\_filename/index.php){
        rewrite (.\*) $1/index.php;
    }
    if (!-f $request\_filename){
        rewrite (.\*) /index.php;
    }
    if (-f $request\_filename/index.html){
        rewrite (.\*) $1/index.html break;
    }
}

location ~ \\.php$ {
  root           C:\\wamp\\www\\blog;
  fastcgi\_pass   127.0.0.1:9000;
  fastcgi\_index  index.php;
  fastcgi\_param  SCRIPT\_FILENAME  $document\_root$fastcgi\_script\_name;
  include        fastcgi\_params;
  proxy\_set\_header Host $host;
  proxy\_set\_header X-Real-IP $remote\_addr;
  proxy\_set\_header REMOTE-HOST $remote\_addr;
  proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;
}

}

-------------本文结束  感谢您的阅读-------------
下次一定