0

Consider StackOverflow, where each question has a unique ID, but URLs are often overridden to include a stub in the URL. For readability and other reasons the stub helps users know they are at the right place.

I have a site that returns 200 when calling a URL like:

http://stackoverflow.com/questions/28057406/

But want the URL to update to:

http://stackoverflow.com/questions/28057406/is-it-possible-to-return-http-code-200-but-give-a-better-url-without-using-3x

The first call is technically valid and the code can retrieve the object and render it perfectly fine, but I'd like to update the URL to use the stubified one.

I'd prefer to do this without a redirect as just getting the ID causes a database call to get the object. Which would mean with a redirect the process would be:

  1. Call http://stackoverflow.com/questions/28057406/
  2. Retrieve item 25257999 from the database to get the name to make the stub
  3. Redirect to http://stackoverflow.com/questions/28057406/is-it-possible-to-return-http-code-200-but-give-a-better-url-without-using-3x
  4. New HTTP Call, so retrieve item 25257999 from the database to render the final page.

If possible I'd like to not use Javascript either.

So, is it possible to return Location as part of a HTTP header with a status code of 200 and the actual page, or am I stuck using 3xx calls or Javascript?

1 Answers1

2

If you are just doing HTTP, you can either choose to redirect, or not choose to redirect... You can also (with Content-Location) tell the client that the canonical address is actually somewhere else... but no browser will respond to that.

To avoid the database-call, you could of course just cache the result.

If you are in a browser however, you can dynamically update the current address without forcing a refresh, with window.history.pushState.

For more information about that call, see this other SO answer:

Modify the URL without reloading the page

Community
  • 1
  • 1
Evert
  • 93,428
  • 18
  • 118
  • 189