Score:0

nginx: How to rewrite a request with a custom status code? (200 instead of 301)

nr flag

For SEO purposes I was requested to configure redirects. Redirecting with a 30x status code is no problem and works:

        location ^/home$ {
              return 301 /foo/bar/index.html;
        };

But it seems like 301 redirects are not sufficient for crawling the page. So I was trying something like this:

        location ^/home$ {
              return 200 /foo/bar/index.html;
        }

But this results in just the string "/foo/bar/index.html" as response. Is there a possibility to redirect a request with a 200 status code?

Richard Smith avatar
jp flag
Try: `try_files /foo/bar/index.html =404;`
sv flag
Welcome to ServerFault. No, it is not possible. All redirects result in 3xx code. Ref: https://wikipedia.org/wiki/List_of_HTTP_status_codes
dbckr avatar
nr flag
@PothiKalimuthu thank you for your answer! I expected that. The developers used a different approach to map the uris
sv flag
That's good to know. Care to share the solution provided by the developers?
Score:0
nr flag

There is probably no solution to configure nginx for this answer. Http just doesn't work like that. In our case we mapped the uri's inside the application and avoided using nginx for rewriting the uris

Score:0
br flag
location ^/home$ {
    rewrite ^/home$ /foo/bar/index.html last;
    return 200;
}

The "rewrite" directive is used to change the requested URL from "/home" to "/foo/bar/index.html". The "last" parameter tells nginx to stop processing the current set of location directives and proceed with the rewritten URL. Then, the "return" directive is used to set the status code to 200.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.