Nginx redirect all requests from subdirectory to another subdirectory root

后端 未结 3 1630
名媛妹妹
名媛妹妹 2020-12-31 06:16

I\'m quite new to Nginx so please bear with me.

I\'m trying to redirect all requests from one subdirectory (store) to the root of another subdirectory (trade). See

3条回答
  •  佛祖请我去吃肉
    2020-12-31 06:49

    You need to ensure that your location ~ \.php$ handler does not take any URLs below the old folder. Indeed, precedence rules are clearly documented within http://nginx.org/r/location, and you can either use regular expressions, or, better yet, use prefix-based matching with the ^~ modifier to instruct that the search must stop without trying to see if that regex-based \.php$ location would match:

        location ^~ /old/long/path/ { # will match /old/long/path/index.php, too
            rewrite  ^/old/long/path/(.*)$  /new/$1  permanent;
        }
    

    The above snippet is likely the most efficient way of doing this, but here is another way of doing the same:

        location ~ /old/long/path/(.*) {
            return  301  /new/$1$is_args$args;
        }
    

    Why does one example has $is_args$args and the other one doesn't? Good question! Note that location directive as well as the first parameter of the rewrite directive both operate based on the contents of the $uri variable, as opposed to $request_uri. Long story short, but $uri does not contain $args, so, in both cases, $1 will not contain any args; however, in the case of rewrite, the case is deemed so common that $args are automatically added back by nginx, unless the new string ends with a ? character, see http://nginx.org/r/rewrite.

提交回复
热议问题