如何在nginx中重定向单个URL?

tok*_*mak 106 rewrite nginx

我正在重组网址结构.我需要为特定网址设置重定向规则 - 我正在使用NGINX.

基本上是这样的:

http://example.com/issue1 --> http://example.com/shop/issues/custom_issue_name1
http://example.com/issue2 --> http://example.com/shop/issues/custom_issue_name2
http://example.com/issue3 --> http://example.com/shop/issues/custom_issue_name3
Run Code Online (Sandbox Code Playgroud)

谢谢!

Moh*_*ady 129

location ~ /issue([0-9]+) {
    return 301 http://example.com/shop/issues/custom_isse_name$1;
}
Run Code Online (Sandbox Code Playgroud)


Bra*_*ncy 116

把它放在你的服务器指令中:

location /issue {
   rewrite ^/issue(.*) http://$server_name/shop/issues/custom_issue_name$1 permanent;
 }
Run Code Online (Sandbox Code Playgroud)

或复制它:

location /issue1 {
   rewrite ^/.* http://$server_name/shop/issues/custom_issue_name1 permanent;
}
location /issue2 {
   rewrite ^.* http://$server_name/shop/issues/custom_issue_name2 permanent;
}
 ...
Run Code Online (Sandbox Code Playgroud)


Col*_*ney 34

如果您需要复制多个重定向,可以考虑使用地图:

map $uri $redirect_uri {
    ~^/issue1/?$    http://example.com/shop/issues/custom_isse_name1;
    ~^/issue2/?$    http://example.com/shop/issues/custom_isse_name2;
    ~^/issue3/?$    http://example.com/shop/issues/custom_isse_name3;
    # ... or put these in an included file
}

location / {
    try_files $uri $uri/ @redirect-map;
}

location @redirect-map {
    if ($redirect_uri) {  # redirect if the variable is defined
        return 301 $redirect_uri;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这就是我在这里寻找的东西 - 将它们放在一个包含的文件中是一个很好的方法来从apache替换我的.htaccess文件充满RewriteRules. (4认同)
  • 您如何将此地图方法与现有位置/ ... proxy_pass类型设置相结合? (3认同)