Nginx下WordPress的Rewrite
本文最后更新于 5696 天前,其中的信息可能已经有所发展或是发生改变。

Apache
在Apache下,利用mod_rewrite来实现URL的静态化。

.htaccess的内容如下:

# BEGIN WordPress
<ifmodule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
# END WordPress

Nginx
在上次《Nginx的Rewrite配置》中有个朋友问WordPress如何配置Rewrite,当时也没给个完整正确的答案,最近自己需要Nginx下配置,不得不去解决这个问题。

其实在Nginx下配置WordPress的Rewrite还是比较简单的,在location /{..................}里面加入
if (!-f $request_filename){
rewrite (.*) /index.php;
}
即可实现。

下面是一个完整的vhost的配置文件

server {
listen 80;
server_name ccvita.com www.ccvita.com;
location / {
        index index.html index.htm index.php;
        root /www/wwwroot/ccvita.com;
        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;
        }

}
location ~ \.php$ {
        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:8787;
        fastcgi_param SCRIPT_FILENAME /www/wwwroot/ccvita.com$fastcgi_script_name;
        }
location /ccvita-status {
        stub_status on;
        access_log off;
        }
}

相关文档
Nginx下Discuz!的Rewrite》:https://kimi.pub/348.html
Nginx下WordPress的Rewrite》:https://kimi.pub/336.html
Nginx的Rewrite配置》:https://kimi.pub/319.html
Nginx的防盗链配置》:https://kimi.pub/312.html

评论

  1. 16年前
    2008-8-14 20:05:30

    if (!-e $request_filename) {
    rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2 last;
    rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
    rewrite ^ /index.php last;
    }

    我的是这样配置的。

  2. joyqi
    16年前
    2008-8-15 11:37:48

    没有评论

  3. 博主
    16年前
    2008-8-16 13:19:56

    @joyqi 看见上面那条评论没有,只是还没有审核而已嘛

  4. 16年前
    2008-8-17 22:00:31

    IIS下好像不行。

  5. 博主
    16年前
    2008-8-17 23:01:55

    @张家口 这不是IIS下的规则嘛~~~~

  6. 16年前
    2008-8-25 10:44:53

    WP可以直接自定议REWRITE

  7. little
    16年前
    2008-8-26 0:59:56

    WP是可以直接自定议REWRITE,但是你服务器得支持啊

  8. 16年前
    2008-9-04 10:20:53

    WP是可以直接自定议REWRITE,但是你服务器得支持啊

  9. miles
    15年前
    2009-2-19 19:04:15

    你好,能帮忙看下我的重写配置有什么问题吗?
    我想用nginx实现和apache里一样的重写规则

    这个是我的.htaccess的重写配置
    Options +FollowSymLinks
    IndexIgnore */*
    RewriteEngine on

    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # otherwise forward it to index.php
    RewriteRule . index.php

    nginx配置
    server {
    listen 80;
    server_name localhost;
    index index.html index.htm index.php;
    root /www/wwwroot;

    location / {
    if (!-e $request_filename){
    rewrite (.*) /index.php;
    }
    }

    location ~ .*\.(php|php5)?$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    access_log off;
    }

    location /status/ {
    stub_status on;
    access_log off;
    }
    }

    • 博主
      miles
      15年前
      2009-2-19 22:43:27

      @miles, 文中有提到在location /{………………}里面加入
      if (!-f $request_filename){
      rewrite (.*) /index.php;
      }
      即可实现。

  10. 14年前
    2010-4-18 22:47:15

    我按照博主写的配置的
    但是结果是
    Error 404 – Not Found

  11. shuiyuai
    14年前
    2010-5-08 22:30:23

    博主你好,我是个新手
    按照博主的方式配置之后,WP固定链接,其他形式都可用
    但是我最想用的postname形式,总是404错误。

    让人郁闷的是,我测试的时候,postname是好使的,把文章都导入后发现出现404了,真是诡异啊
    请博主帮忙分析下原因吧,感激不尽

  12. 13年前
    2010-12-30 6:01:19

    我刚刚安装好cetos5.5系统,安装好LUNM,把WordPress也恢复了,但原来的固定链接不能访问(403错误),

    重新设置固定链接就出现了以前我以为只有在Windows主机才出现的“index.php”,按照网上一些修改nginx.conf的方法,也没有成功。

    请问具体怎么解决!!

    网站根目录/home/wwwroot,WordPress安装在wwwroot的blog下。

    拜托博主帮忙解答~~!!

    (这儿咋没有回复邮件订阅~~)

  13. 13年前
    2010-12-30 6:02:32

    lump,不好意思。

  14. Casey
    13年前
    2011-5-05 0:13:10

    但是你服务器得支持啊

  15. kokis
    12年前
    2012-3-20 9:57:59

    你好,我请教个问题:
    我的网站根目录是/wwwdoc/myweb/
    我配置了域名domian.com指向myweb/可正常访问。
    现在我新建了个目录/wwwdoc/img/,用来存储上传的图片, 比如今天现在上传了一张xxx.jpg的图片,存在服务器上的完整路径是/wwwdoc/img/2012/03/xxx.jpg,
    我希望图片的url是:http://img.domain.com/2012/03/xxx.jpg;
    我的nginx该如何配置?请指点下,谢谢

  16. 12年前
    2012-9-18 11:02:48

    学习了,谢谢分享

  17. 11年前
    2012-11-05 23:09:36

    亲:不但可以购物还可以挣钱,返利网购物50%提成佣金:
    【穿着打扮潮范儿】http://www.taoaoy.com
    【亲:范儿】http://www.jijubuy.com
    【淘玖玖-返利网】http://www.taomiy.com
    【新款一条街】http://www.xdixue.com
    【天天逛街返利网】http://www.tianaoy.com

    网购B2C导航网站比价购物,京东商城,凡客诚品,卓越,减肥,绿色,保健,包包淘宝客,左旋肉碱,雨淘天下,淘宝旗舰店,淘宝网旗舰店,淘宝网旗舰店大全
    【欢乐逛街】http://www.eas7.com
    【逛乐街】http://www.tkxin.com
    【喜欢逛】http://www.jiumy.com

  18. 10年前
    2013-12-08 21:35:39

    相关文档推荐中有bug,这篇文章《Nginx下WordPress的Rewrite》:http://www.ccvita.com/336.html又出现了。

  19. 9年前
    2015-1-10 15:48:25

    博主,你确定你的ccvita-status在WP伪静态规则下能调用???
    http://www.ccvita.com/ccvita-status 都是404呢

  20. 7年前
    2017-1-09 10:50:09

    说的很对。www.hkinsu.com

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇