Basic overview of Rewrite

Basic overview of Rewrite

What is rewrite

Rewrite mainly implements url address rewriting and redirection, which is the process of redirecting incoming web requests to other urls.

rewrite usage scenario

1. Address jump. Users visit www.drz.com. This URL is to direct it to a new domain name, mobile.drz.com.
2. Protocol jump. When a user requests a website through http protocol, he jumps it back to https protocol mode.
3. Pseudo-static technology, which displays dynamic pages as static pages, facilitates the input of search engines, and builds dynamic URL addresses to expose too many parameters to improve security.
4. Search engine, SEO optimization depends on url path, easy to remember url for wisdom tooth search engine input

rewrite configuration example

Syntax: Syntax:  rewrite regex replacement [flag]
//Default: -
//Context: server,location,if

#Used to switch maintenance page scenarios
#rewrite ^(.*)$ /page/maintain.html break;

Rewrite tag Flag

The rewrite directive redirects URL s or modifies strings based on expressions. It can be applied to server, location and if environments. Each line of rewrite directive is followed by a flag tag. The flag tags supported are shown in the following table:

flag Effect
last After this rule matching is completed, stop matching and no longer match the following rules
break After this rule matching is completed, stop matching and no longer match the following rules
redirect Returns 302 temporary redirection, and the address bar displays the jumped address
permanent Returns 301 permanent redirection, and the address bar displays the jumped address

Example of last versus break differences

[root@web01 conf.d]# cat rewrite.conf 
server {
        listen 80;
        server_name rewrite.drz.com;
        root /code;

        location ~ ^/break {
                rewrite ^/break /test/ break;
        }
        location ~ ^/last {
                rewrite ^/last /test/ last;
        }
        location /test/ {
                default_type application/json;
                return 200 "ok";
        }
}

#Restart nginx service
[root@web01 conf.d]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# nginx -s reload

If you understand shell scripts, these two are similar to break and continue in scripts.

Browser access break

Browser access last

The difference between last and break

As long as the break matches the rule, it will go to the directory of the local configuration path to find the requested file.
last, as long as it matches the rule, requests are re-initiated for the server(...) tag it belongs to.

break request:
1. Request rewrite.drz.com/break
 2. First, we will look for the local / code/test/index.html.
3. If found, return the contents of / code/test/index.html.
4. Error 404 if the directory is not found, 403 if the directory is not found.

last request:
1. Request rewrite.drz.com/last
 2. First, we will look for the local / code/test/index.html.
3. If found, return the contents of / code/test/index.html.
4. If not found, a new request will be made to the current server, rewrite.drz.com/test./
5. If there is a location match, return the location content directly.
4. If there is no location match, return to 404.

Therefore, when accessing / break and / last requests, although the corresponding request directory / test does not exist and should theoretically return 404, in fact, when requesting / last, there will be results matched by the latter location returned, because of this.

An example of the difference between redirect and permanent

[root@web01 conf.d]# cat rewrite.conf 
server {
        listen 80;
        server_name rewrite.drz.com;
        root /code;

        location /test {
                rewrite ^(.*)$  http://www.driverzeng.com redirect;
                #rewrite ^(.*)$  http://www.driverzeng.com permanent;
                #return 301 http://www.driverzeng.com;
                #return 302 http://www.driverzeng.com;
        }
}

Distinction between redirect and permanent

redirect: Each request asks the server, and if the server is unavailable, the jump fails.

permanent: The first request will ask, the browser will record the address of the jump, the second will not ask the server, directly through the browser cached address jump.

Rewrite Rule Practice

Before writing rewrite rules, we need to open the rewrite log to debug the matching of rules.

[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;

http{
    rewrite_log on;
}

Case I

User access to / abc/1.html is actually real access to / ccc/bbb/2.html

#http://www.drz.com/abc/1.html  ==>  http://www.drz.com/ccc/bbb/2.html

#1. Preparing the Real Access Path
[root@web03 ~]# mkdir /code/ccc/bbb -p
[root@web03 ~]# echo "ccc_bbb_2" > /code/ccc/bbb/2.html

#2.Nginx jump configuration
[root@web03 ~]# cd /etc/nginx/conf.d/
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;

        location / {
                root /code;
                index index.html;
        }
        location /abc {
                rewrite (.*) /ccc/bbb/2.html redirect;
                #return 302 /ccc/bbb/2.html;
        }
}

#3. Restart the Nginx service
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload

Case 2

User access/2018/ccc/2.html is actually real access to/2014/ccc/bbb/2.html

##http://www.drz.com/2018/ccc/2.html  ==>  http://www.drz.com/2014/ccc/bbb/2.html

#1. Prepare real access paths
[root@web03 conf.c]# mkdir /code/2014/ccc/bbb -p 
[root@web03 conf.c]# echo "2014_ccc_bbb_2" > /code/2014/ccc/bbb/2.html

#2.Nginx jump configuration
[root@web03 conf.d]# cat ccbb.conf 
server {
        listen 80;

        location / {
                root /code;
                index index.html;
        }
        location /2018 {
                rewrite ^/2018/(.*)$ /2014/$1 redirect;
        }
}

#3. Restart nginx service
[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 conf.d]# nginx -s reload

Case 3

User access / test actually accesses https://www.driverzeng.com

#1.Nginx jump configuration
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;

        location /test {
                rewrite (.*) https://www.driverzeng.com redirect;
        }
}

#2. Restart nginx service
[root@web03 conf.d]# nginx -s reload

Case IV

User access to course-11-22-33.html is actually / course/11/22/33/course_33.html

#http://www.drz.com/couese-11-22-33.html  ==>  http://www.drz.com/course/11/22/33/course_33.html

#1. Prepare real access paths
[root@web03 ~]# mkdir /code/course/11/22/33 -p
[root@web03 ~]# echo "curl docs.etiantian.org" > /code/course/11/22/33/course_33.html

#2.Nginx jump configuration
[root@web03 conf.d]# cat test.conf 
server {
        listen 80;
        root /code;
        index index.html;
        location / {
                #Flexible allocation
                rewrite ^/course-(.*)-(.*)-(.*).html$ /course/$1/$2/$3/course_$3.html redirect;
                #Fixed matching method
                #rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
        }
}

#3. Restart nginx service
[root@web03 conf.d]# nginx -s reload

Case V

Jump http requests to https

#Nginx jump configuration
server {
        listen 80;
        server_name www.dirverzeng.com;
        rewrite ^(.*) https://$server_name$1 redirect;
        #return 302 https://$server_name$request_uri;
}       

server {
        listen 443;
        server_name www.driverzeng.com;
        ssl on;
}

Rewrite scenario example

#Deployment of discuz Forum
[root@web01 conf.d]# vim discuz.drz.com.conf
server {
        listen 80;
        server_name discuz.drz.com;

        location / {
                root /code/discuz;
                index index.php index.html;
        }

        location ~ \.php$ {
                root /code/discuz;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

#Create site directory deployment code
[root@web01 ~]# mkdir /code/discuz
[root@web01 ~]# rz Discuz_X3.3_SC_GBK.zip
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/

#Authorized Site Directory
[root@web01 discuz]# chown www.www -R /code/

#Create a database
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on discuz.* to discuz@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

server {
        listen 80;
        server_name discuz.drz.com;

        location / {
                root /code/discuz/upload;
                index index.php index.html;
                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/archiver/index.php?action=$2&value=$3 last;
                if (!-e $request_filename) {
                    return 404;
                }
        }

        location ~ \.php$ {
                root /code/discuz/upload;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

Rewrite rule supplement

rewrite matching priority

1. Execute the rewrite instruction of server block first
2. Next, the location matching rule is executed
3. Finally, rewrite in location is executed

rewrite and nginx global variables

Rewrite uses some Nginx global variables in the matching process

$server_name    #The domain name requested by the current user

server {
        listen 80;
        server_name test.drz.com;
        rewrite ^(.*)$ https://$server_name$1;
}
$request_filename Request file path name (home directory with website)/code/images/test.jpg)
$request_uri Current requested file path (home directory without website)/inages/test.jpg)

#Most of them are used for http protocol to gttps protocol
server {
        listen 80;
        server_name php.drz.com;
        return 302 https://$server_name$request_uri;
}
$scheme Used protocols, such as http perhaps https

How to Standardize Writing rewrite Rules

server {
        listen 80;
        server_name www.drz.com drz.com;
        if ($http_host = drz.com){
            rewrite (.*) http://www.drz.com$1;
        }
}

#Recommended Writing Format
server {
        listen 80;
        server_name drz.com;
        rewrite ^ http://www.drz.com$request_uri;
}
server {
        listen 80;
        server_name www.drz.com;
}

Tags: MySQL Nginx PHP vim Database

Posted on Mon, 02 Sep 2019 10:41:24 -0400 by promovi