Redirection: Location vs Refresh

Asked

Viewed 664 times

10

When to use Location and Refresh to redirect.

    header( "Location: www.dominio.com" , TRUE , 302 )
    header( "Refresh:5; url=www.dominio.com" , TRUE , 302 )

Both options produce the same result: redirecting. The difference is that using Location redirecting is instantaneous, while Refresh a delay can be set for redirecting.

I am wanting to understand the differences between the two cases, when opting for one or the other. Since Refresh has the advantage of choosing time, I see no sense in Location.

One can define the status 3xx in both cases. The status-code is who reports the redirect cases, so Location and Refresh are only a means to an objective - are indifferent?

  • I’m curious now. refresh changes history the same way redirect?

  • Something related to in that matter, But to tell you the truth, I didn’t quite understand.

  • Maybe that question answer your question

  • 1

    I think this issue is not only related to [tag:php], maybe changing the tag to [tag:http] is more coherent.

  • Thanks @gmsantos, changed the tag.

2 answers

9


The two functions serve to redirect/refresh a page, however they have different ways of working.

With the Location the browser does not have to download all the content of the page before making the redirect. This way there will be no problems in using the back button of your browser as the function is made server-side.

With the function of Refresh you will be sending a request to the browser ( client-side) to update the page. That is, the browser will first download the page and then the time set in the meta tag will redirect the page. Also, if you click "Back" in the browser it will not work as it should, as it will go back to the page where it just left and will be redirected again.


Location

  • Server-side
  • Do not download the site before redirecting

Refresh

  • Client-side
  • You can set time to send to next page
  • It may disturb the proper functioning of the back button of the browser
  • Download content from sites before redirecting

For more information: http://www.w3.org/QA/Tips/reback

  • 1

    Perfect! Explains every feature very well. Now it’s easier to decide when to use each case.

  • @Leandro Godoy Rosa, I could tell you more about this?

  • 1

    Checking his answer better I saw that I interpreted it a little wrong, now I realized that he refers to Location as the header, while Refresh as the tag meta while I considered the two to be the header, since your question was about the headers and not the tag meta. When it comes to headers I think it’s wrong to say that one is server-side and the other client-side, forwarding with the 3XX status code is considered server-side but depends on the client-side implementation, who actually opens the new address is the client

  • But anyway I think this answer is not correct for the question since the question itself was about the headers Location and Refresh, and the answer was about the header Location and the use of the meta tag to make the refresh

  • 1

    A detail that I think important as well and does not have in any of the answers, the Refresh header is actually not part of the HTTP/1.1 specification, if I am not mistaken it was created at the time of Netscape and is still supported by browsers. If you check on RFC7231 from HTTP/1.1 it says that a 302 redirect for example MUST contain the Location field, and Refresh is nowhere in RFC

  • @Leandrogodoyrosa I knew this, but I had no basis to prove, elaborate an answer with this, it is very cool

Show 1 more comment

2

To discover the difference we will have to analyze this question from two points of view, server-side and client-side:

Server-side

For the server there is no difference, it will simply be another HTTP response with the 302 status which in the specification is redirect status. " What do you mean there’s no difference?" That’s right for the server that matters is the request the response is of interest to the client (disregarding http gateways) so here we realize we’ll have to analyze this client-side problem!

Client-side

Now that we know that the problem is here we have to understand one thing about the HTTP client: For the HTTP client no matter what status and header the server sends, the answer will always be downloaded to the client, the responsibility of sending a body or is not on the server, As much as the answer is in the client’s interest, he can’t do it. Knowing this we realize that the difference is not in downloading or not the content of the answer!
At these times you will ask: "Okay, so what’s the difference?". And I would have to answer: "Which is your HTTP client? A browser? A Javascript client? A PHP client? How is it implemented?". What is done with the response headers is the decision of the HTTP client! So without knowing your client has no way of knowing how the behavior will be for each answer.

Completion

We realize that the difference will manifest itself in the implementation of the HTTP client, so I can’t answer precisely what the difference is! But in general the difference is that the header Refresh was created to display the content of the response before the redirect, already the header Location does not show any content however you have it.

  • Another great answer. I just don’t understand one thing. Refresh and Location have a difference between browser and javascript, or the JS case was an example of a status header for an asynchronous request?

  • It is that when you perform an AJAX for example javascript does not perform an automatic follow redirect, unless you implement this, in some current browsers this is no longer true... So even if you respond with 302 status and header Location if your javascript client hasn’t implemented follow redirect nothing will happen! Already in the browsers will depend on each one, each browser implements in its own way, thankfully that the popular browsers follow relatively well the HTTP specifications =)

  • My case is via browser. Thanks for the explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.