nginx jump with parameters

Keywords: Linux PHP Nginx Apache REST

//Original link: https://www.baidu.com/benefit_detail?slug=bankofchina-20170320
//Target link: https://www.test.cn/boc 
location ~ /benefit_detail {
        if ($args ~* "slug=bankofchina-20170320") {
            rewrite ^/benefit_detail /boc? permanent;
        }
        try_files $uri $uri/ /index.php?$query_string;
        }
Common jump cases:
1,take www.myweb.com/connect Jump To connect.myweb.com

rewrite ^/connect$ http://connect.myweb.com permanent;

rewrite ^/connect/(.*)$ http://connect.myweb.com/$1 permanent;

2,take connect.myweb.com 301 Jump To www.myweb.com/connect/ 

if ($host = "connect.myweb.com"){

rewrite ^/(.*)$ http://www.myweb.com/connect/$1 permanent;

    }

3,myweb.com Jump To www.myweb.com

if ($host != 'www.myweb.com' ) { 

rewrite ^/(.*)$ http://www.myweb.com/$1 permanent; 

    }

4,www.myweb.com/category/123.html Jump to category/?cd=123

rewrite "/category/(.*).html$" /category/?cd=$1 last;

5,www.myweb.com/admin/ Jump down to www.myweb.com/admin/index.php?s=

if (!-e $request_filename){

rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;

    }

6,Add after/index.php?s=

if (!-e $request_filename){

    rewrite ^/(.*)$ /index.php?s=/$1 last;

    }

7,www.myweb.com/xinwen/123.html  etc. xinwen Number below+html Link Jump of 404

rewrite ^/xinwen/([0-9]+)\.html$ /404.html last;

8,http://www.myweb.com/news/radaier.html 301 Jump to http://www.myweb.com/strategy/

rewrite ^/news/radaier.html http://www.myweb.com/strategy/ permanent;

9,Redirect link to 404 pages

rewrite http://www.myweb.com/123/456.php /404.html last;

10, prohibit htaccess

location ~//.ht {

         deny all;

     }

11, Can be prohibited/data/Under Multilevel Directory.log.txt Etc. request;

location ~ ^/data {

     deny all;

     }

12, Prohibit single file

location ~ /www/log/123.log {

      deny all;

     }

13, http://www.myweb.com/news/activies/2014-08-26/123.html Jump to http://www.myweb.com/news/activies/123.html

rewrite ^/news/activies/2014\-([0-9]+)\-([0-9]+)/(.*)$ http://www.myweb.com/news/activies/$3 permanent;

14,nginx Multiple Conditional Redirection rewrite

//Jump to play if you need to open a link with play, but/admin/play cannot

        if ($request_filename ~ (.*)/play){ set $payvar '1';}
        if ($request_filename ~ (.*)/admin){ set $payvar '0';}
        if ($payvar ~ '1'){
                rewrite ^/ http://play.myweb.com/ break;
        }

15,http://www.myweb.com/?gid=6 Jump to http://www.myweb.com/123.html

 if ($request_uri ~ "/\?gid\=6"){return  http://www.myweb.com/123.html;}

//Regular expressions match, where:

* ~ Case sensitive matching

* ~* For case insensitive matching

* !~and!~*Case-sensitive mismatch and case-insensitive mismatch, respectively

//Files and directories match, where:

* -f and!-f Used to determine if a file exists

* -d and!-d Used to determine if a directory exists

* -e and!-e Used to determine whether a file or directory exists

* -x and!-x Used to determine whether a file is executable

flag Marked as:

* last Amount to Apache In[L]Tag, indicating completion rewrite

* break Terminate Matching, No longer matches the following rules

* redirect Return to the 302 temporary redirection address bar to show the jumped address

* permanent Returning 301 permanent redirection address bar will show the jumped address
nginx Refer to the meaning of each built-in variable:
https://blog.csdn.net/wanglei_storage/article/details/66004933
[Understanding of nginx try_files]
Take try_files $uri $uri/ /index.php; for example, when a user requests http://servers.blog.ustc.edu.cn/example,
Here, $uri is/example.try_files will try to find the file on your hard drive.If there is a name /$root/example (where $root is WordPress) 
The contents of this file are sent directly to the user.Obviously, there is no example in the directory
 File.Then look at $uri/, add a/, that is, see if there is a directory named /$root/example/.No more,
It will fall back to the last option/index.php of try_files and initiate an internal "sub-request", which is equivalent to
 nginx initiates an HTTP request to http://servers.blog.ustc.edu.cn/index.php.This request will be located
 ~\php${...} catch resides, which is the process that enters FastCGI.The specific URI and parameters are in REQUEST_URI 
Passed to FastCGI and WordPress programs in, so unaffected by URI changes
[$request_uri explanation]
$request_uri is the first part of the complete url to be shaved out of the rest of $host, such as http://www.baidu.com/pan/beta/test1?fid=3.
Remove www.baidu.com and you'll see in the log that the printed $request_uri is actually/pan/beta/test1?fid=3.
If you only visit www.baidu.com, $request_uri also has one/

if ($request_uri ~*'^/$') means that there is only a domain name in the url, followed by nothing, such as www.baidu.com.
if ($request_uri ~* "test") means that the string after the domain name can be matched successfully if it contains the keyword test.
For example, www.baidu.com/pan/beta/test3

if ($request_uri ~* "^/$"){
               rewrite ^ http://kj.fnwtv.com/index.html permanent;
           }

if ($request_uri !~* "^/$") {
                rewrite ^ http://www.fnwtv.com/ permanent;
            }
[Nginx if Conditional Judgment Reference)
https://www.cnblogs.com/saneri/p/6257188.html
location proxy_pass Hinder url Add or Not/Differential reference for:
https://blog.51cto.com/huangzp/1954575

Posted by dingus on Sun, 05 Jan 2020 18:42:22 -0800