lnmp 二级目录安装discuz、wordpress的解决方案

今天这个问题困扰了我一下,以前很少在子目录建站,这次因为想把网站集中一下。所以折腾很久。由于看到网上很多文章并没有解决我的问题,很多都是二级目录安装同样的程序。废话不多说,我总结了几种情况。

时间一久,这些都会变成思路,请参考,注意自己的情况。我用的是lnmp

一、根目录是wordpress程序,二级目录blog需要再安装wordpress

①修改wordpress.conf为下面内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
location / {
    try_files $uri $uri/ /index.php?$args;
}


# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location /blog/ {
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 (.*) /blog/index.php;
        }
}

其实就是在之前的基础上增加了以下代码

1
2
3
4
5
6
7
8
9
10
11
location /blog/ {
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 (.*) /blog/index.php;
        }
}

上面的代码复制了,记得修改下blog。blog是我的二级目录名称。

②lnmp现在的版本其实提供了一个wp2.conf文件(/usr/local/nginx/conf/rewrite/目录下),里面就是上面的方法,你可以修改wp2.conf里面目录的名称,默认是wp。然后在你网站域名的配置文件domain.conf里面引入

1
2
include wordpress.conf;
include wp2.conf;

二、根目录是discuz程序,新建blog为二级目录安装wordpress。

discuzx.conf的文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
location / {

rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
if (!-e $request_filename) {
    return 404;
}
}

新建一个blog.conf,放入以下代码

1
2
3
4
5
6
7
8
9
10
11
12
location /blog/ {
index index.html index.php;
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 (.*) /blog/index.php;
}
}

相当于指定了目录。

你网站域名的配置文件domain.conf里面引入

1
2
include discuzx.conf;
include blog.conf;

如果你用的是dz 7.2就是

1
2
include discuz.conf;
include blog.conf;

三、根目录是wordpress程序,新建bbs为二级目录安装discuz

引入wordpress.conf和discuzx2.conf

1
2
include wordpress.conf;
include discuzx2.conf;

discuzx2.conf的内容为以下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
location /bbs/ {
    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
    rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
    rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
    rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
    rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
    rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
    if (!-e $request_filename) {
        return 404;
    }
}

wordpress.conf的内容不变。

四、如果你根目录是discuz、想再建立一个pianpai目录再建一个论坛。

那么你网站域名的配置文件domain.conf里面引入discuzx.conf和discuzx2.conf

1
2
include discuzx.conf;
include discuzx2.conf;

这时候你的discuzx.conf内容应该为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
location / {
    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
    rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
    rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
    rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
    rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
    rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
    if (!-e $request_filename) {
        return 404;
    }
}

discuzx2.conf内容应该为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
location /bbs/ {
    rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
    rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
    rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
    rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
    rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
    rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
    rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
    rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
    if (!-e $request_filename) {
        return 404;
    }
}

发表评论

:?: :razz: :sad: :!: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: :smile: :evil:
贴图 表情 ( ps. 若要贴代码, 请將 "<" 改成 "&lt;" 即可. )

5 + 5 = ?

这篇文章上的评论的RSS feed TrackBack URL