W
W
Win32Sector2019-01-20 13:58:50
Nginx
Win32Sector, 2019-01-20 13:58:50

Is it possible to rewrite rewrites easier?

Good day.
Now I am rewriting apache configs to nginx.
And my question is, can it be made easier?
Here is an example of two RewriteRule apache:

RewriteCond %{HTTP_HOST} ^www.example.com [NC]
    RewriteCond %{REQUEST_URI} /sitemap.xml [NC]
    RewriteRule .* sitemap/example/desktop/sitemap.xml [L]
    
    RewriteCond %{HTTP_HOST} ^example [NC]
    RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]

Here I rewrote them for nginx:
if ($http_host ~* "^www.example.com"){
    rewrite ^(.*)$ /sitemap/example/desktop/sitemap.xml break;
     }
     
    if ($http_host ~* "^example"){
    rewrite ^(.*)$ https://www.example.com$request_uri redirect;
    }

Is it possible to somehow do without if?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-01-21
@dodo512

server {
    server_name example.com;

    return https://www.example.com$request_uri;
}

server {
    server_name www.example.com;
    
    location = /sitemap.xml {
        try_files /sitemap/example/desktop/sitemap.xml =404;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question