We already have many customers using Nginx on VPS. One of the most common problems encountered by customers who have just transferred from Apache is how to rewrite under Nginx and how to convert .htaccess in Apache into Nginx. There are many online articles about this. There is a lot of information, including rewriting on WordPress, discuz, phpcms, ecshop, shopex, etc., just copy it. Another common problem for Nginx novices is that they don’t know how to change the rewrite rules after getting them. For example, what should the rewrite of the subdirectory under Nginx be changed to? / is wordpress, /bbs is for discuz, / is discuz, /blog is for wordpress, or / is for wordpress, and /blog is for wordpress, etc. How to change this rewrite? Let’s put a few examples in our FAQ for reference:
WordPress is installed in the subdirectory /blog:
location /blog/ {
root /home/www/vpsee.com;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^.+/?(/blog/wp-.*) $1 last;
rewrite ^.+/?(/blog/.*.php)$ $1 last;
rewrite ^(.+)$ /blog/index.php?q=$1 last;
}
}
Discuz! 7.2 is installed in the subdirectory /bbs:
location /bbs/ {
root /home/www/vpsee.com;
index index.php index.html index.htm;
rewrite ^/bbs/archiver/((fid|tid)-[w-]+.html)$ /bbs/archiver/index.php?$1 last;
rewrite ^/bbs/forum-([0-9]+)-([0-9]+).html$ /bbs/forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/bbs/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ /bbs/viewthread.php?tid=$1&extra=page %3D$3&page=$2 last;
rewrite ^/bbs/space-(username|uid)-(.+).html$ /bbs/space.php?$1=$2 last;
rewrite ^/bbs/tag-(.+).html$ /bbs/tag.php?name=$1 last;
}
Discuz! X1.5 is installed in the subdirectory /bbs:
location /bbs/ {
root /home/www/vpsee.com;
index index.php index.html index.htm;
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]+).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 ^([^.]*)/([az]+)-(.+).html$ $1/$2.php?rewrite=$3 last;
if (!-e $request_filename) {
return 404;
}
}
If you have difficulty understanding regular expressions like ^([^.]*)/([az]+)-(.+).html$ and are interested in this aspect, you can read some books, preferably The best book should be Mastering Regular Expressions by O'Reilly (also available in Chinese version: "Mastering Regular Expressions").
Article source: www.vpsee.com
【Related articles】
Nginx commonly used pseudo-static rules collect Discuz and other pseudo-static rules