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".
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.
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.
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.)
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.
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:
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.
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/" />
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With