Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP redirect: 301 (permanent) vs. 302 (temporary) in Spring

I want to make a 301 redirect in Spring, So here the piece of code I use

@RequestMapping(value = { "/devices" } , method = RequestMethod.GET)
    private String initGetForm(@ModelAttribute("searchForm") final SearchForm searchForm,
                            BindingResult result, 
                            HttpServletRequest request,
                            HttpServletResponse response,
                            Model model, Locale locale) throws Exception {


        String newUrl = "/devices/en";

        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        response.setHeader("Location", newUrl);
        response.setHeader("Connection", "close");

        return "redirect:" + newUrl;

}

But checking the IE Developer Tools I got this Status 302 Moved Temporarily !

like image 611
Nunyet de Can Calçada Avatar asked May 12 '16 09:05

Nunyet de Can Calçada


People also ask

Where would you use 301 or permanent redirect and 302 or temporary redirect?

The Difference Between a 301 Redirect vs. 301 redirects are permanent, whereas 302 redirects are temporary. A 301 is used when a page has permanently changed location, and a 302 should be used if you intend to move the page back under the original URL in the future.

Which is better 301 or 302 redirect?

For permanent changes to a website and continued ranking through SEO, a 301 redirect is necessary. On the other hand, if you're only performing a temporary change, a 302 redirect is better. It tells the search engine that the changes are temporary and may not impact the original page's SEO ranking.

When should you use a 302 redirect?

When Should You Use 302 Redirects? Use this type of redirect if you want to send users to a new site or page for a short period of time, such as when you're redesigning or updating your website. Only use a 302 if you're planning on eventually bringing the old page back or setting up a new one.

How long does a 302 redirect last?

Unlike 301 pages, 302 redirects are temporary, which means you can switch back at any time.


1 Answers

Spring is resetting your response headers when it handles the redirection since you are returning a logical view name with a special redirect prefix.If you want to manually set the headers handle the response yourself without using Spring view resolution. Change your code as follows

@RequestMapping(value = { "/devices" } , method = RequestMethod.GET)
private void initGetForm(@ModelAttribute("searchForm") final SearchForm searchForm,
                                BindingResult result, 
                                HttpServletRequest request,
                                HttpServletResponse response,
                                Model model, Locale locale) throws Exception {

            String newUrl = request.getContextPath() + "/devices/en";
            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", newUrl);
            response.setHeader("Connection", "close");

    }
like image 135
ekem chitsiga Avatar answered Nov 15 '22 04:11

ekem chitsiga