Rewrite url using rewrite under the Nginx server to implement the pseudo static example

  • 2020-05-14 05:40:11
  • OfStack

After searching and testing on the Internet, it is found that the Rewrite rules of Nginx and Rewite rules of Apache are not very different, and can be used almost directly. For example, in Apache you write the rule like this


rewrite ^/([0-9]{5}).html$ /viewthread.php?tid=$1 last;

Writing in Nginx like this does not start it. The solution is to put two double quotation marks:


rewrite "^/([0-9]{5}).html$" /viewthread.php?tid=$1 last;

At the same time, RewriteRule is changed into Rewrite, which basically realizes the conversion from Rewrite rule of Nginx to Rewite rule of Apache.

The Rewrite Flags

last - basically Flag. break - abort Rewirte, no longer match redirect - returns the HTTP state of temporary redirection 302 permanent - returns permanent redirection to HTTP state 301

The WordPress Rewrite
Under the Nginx configuration WordPress Rewrite or are simple, in location / {... } inside


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

Can be implemented.

Below is a complete vhost configuration file


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;
    }
}

Discuz! The Rewrite
The following Rewrite percent sign is preceded by a transition character "\", which is required in Apache but not in Nginx.


rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page\%3D$3&page=$2 last;

The correct answer is


rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;

This error exists on virtually all current websites that use Nginx as a server and have Rewrite enabled. Including Discuz! Officially, it has given feedback to cnteacher so far.
Nginx instance code


server {
  listen    80;
  server_name www.ccvita.com ccvita.com;

  location / {
     index      index.html index.htm index.php;
     root      /www/www.ccvita.com;
     rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
     rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
     rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
     rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
     rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
     rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;

  }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:8694;
        fastcgi_param SCRIPT_FILENAME /www/www.ccvita.com$fastcgi_script_name;
    }

  location /www.ccvita.com-status {
     stub_status on;
     access_log off;
  }
}


Related articles: