1

I have the following nginx redirect which needs to redirect all requests to a different domain name. It works fine.

However the request url will be like: https://example.com/abc/123/TOKEN

How can I reformat this in the redirect to be like: https://example.com/TOKEN

events {}
http {
server {
    listen       80  default_server;
    server_name  _;
    return 301 https://example.com/$request_uri;
}
}
0

1 Answer 1

1

The Nginx rewrite...permanent; statement can change the URL before generating a 301 redirect.

For example:

server {
    listen 80 default_server;
    rewrite ^/[^/]+/[^/]+(/.*)$ https://example.com$1 permanent;
}

Note: server_name _; is no longer necessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.