简体   繁体   中英

Converting .htaccess rewrites to NGINX configuration

I am having difficulties converting two RewriteCond 'blocks' of rewriterules to NGINX configuration, and would like a fresh view on this. The blocks of rewriterules I am trying to convert to NGINX look like this:

# Production
RewriteCond %{HTTP_HOST} ^www\.domain\.nl$ [NC]
RewriteRule ^(_next/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(assets/next/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(lp2/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(advice/welcome)$ https://lp.domain.nl/$1 [P]
RewriteRule ^(RewriteRule ^(api/.*)$ https://lp.domain.nl/$1 [P]
RewriteRule ^api/v1/(.*)$ https://app.domain.nl/api/v1/$1 [P]

I would recommend capturing the https redirect paths using a map. Using regular expressions in a map minimizes their impact on the flow of your config.

map $uri $redirect_https {
    ~^/(?<path>_next/.*) lp.domain.nl/$path;
    ~^/(?<path>assets/next/.*) lp.domain.nl/$path;
    ~^/(?<path>lp2/.*) lp.domain.nl/$path;
    ~^/(?<path>api/v1/.*)$ app.domain.nl/$path;

    # default api check should come last
    ~^/(?<path>api/.*) lp.domain.nl/$path;
}

server {
    server_name www.domain.nl;
    listen 80;

    location = /advice/welcome {
        return 301 https://lp.domain.nl/advice/welcome;
    }

    if ($redirect_https) {
        return 301 https://$redirect_https;
    }

    # ... other non-https config here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM