Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show placeholder page when browser receives 302 request

When a browser is receiving redirect request from server, for a fraction of second, browser shows an error page that says "Page not found", and then redirects to appropriate url.

I am looking for solution where browser, instead of displaying "Page not found" page, shows a dummy page.

I assume this is what payment gateways are doing by displaying a page that says "Do not press back/refresh button".

like image 540
Aki008 Avatar asked Aug 23 '17 10:08

Aki008


People also ask

What happens when a server responds with a 302 status code?

When a server responds with a 302 status code, it send back the newer url (to which the current requested old url is to be redirected) to the requesting user-agent (likely a browser). Now, as per the RFC document, the user agent must not request the newer url for 302 status code.

Does the browser automatically redirect to 302 when making a request?

Thanks in advance. @kayazinc If you make a request via axios (or the XMLHttpRequest directly), the browser will not automatically redirect on 302. You need to implement this logic in your application.

What is the path of a browser request?

The Path of a Browser Request. There are a handful of general steps that occur between the time you request a web page and the time it displays in your browser. Browser sends additional requests for objects embedded in the html file (CSS files, images, javascript, etc.)

How do I request a page from a server?

The latter is how servers are located though, so the first step in requesting a page is converting the domain to an IP address. Several hundred root servers are scattered around the world. Each contains information that maps every domain to the IP address of the server on which it’s located.


4 Answers

I believe your initial premise that when a browser receives a 302 response code it shows an error, might be incorrect.

You can see redirects happening in many websites where all the browser does is just change the address in the address bar and load the eventual page, without displaying anything in the mean time.

Moreover, the 302 HTTP status code is not intended to have a body part, and if it has one it is usually ignored. And so, I can think of only two things that may be happening:

  1. You're using some browser that has a different behavior when accepting a 302 code (older?).
  2. The server isn't sending a 302, and in fact is sending back a page with redirection code in it.

A good way to check this depends on which browser you're using, but most modern browsers have a 'developer' pane where you can see the outgoing requests and incoming responses and their headers and status codes. You can then verify what is actually happening.

If the case is the former, then I'm afraid there's probably not much you can do about it without altering the browser itself. If it is the latter, then assuming the server code is under your control, you can change whatever content is returned.

like image 71
Kraylog Avatar answered Oct 13 '22 15:10

Kraylog


I assume this is what payment gateways are doing by displaying a page that says "Do not press back/refresh button"

To achieve this you can use meta tag to refresh / redirect. The following snippet is taken from this SO post

<meta http-equiv="refresh" content="3;url=http://www.google.com/" />
like image 30
riyaz-ali Avatar answered Oct 13 '22 14:10

riyaz-ali


You can add a Custom error page

To map 302 to a static HTML file, you need to create an html file called 302.html under resources/public/error, your folder structure should look like:

src/
 +- main/
     +- java/
     |   
     +- resources/
         +- public/
             +- error/
                 +- 302.html
like image 1
juanlumn Avatar answered Oct 13 '22 15:10

juanlumn


If you are using Spring (as the tag suggests) you can have a class to handle the redirection, which redirects you immediately to the page you desire.

There are multiple different ways to handle redirection, here you have a good guide: http://www.baeldung.com/spring-redirect-and-forward

like image 1
ricol070 Avatar answered Oct 13 '22 14:10

ricol070