Tony the Pony
Tony the Pony

Reputation: 41357

Redirect to a new URL without browser issuing a second request?

Is there a way for a web server to issue a redirect that does not require the browser to issue a second request?

For example, the browser requested http://www.mysite.com/lion, but the server wants the browser to change location to http://www.mysite.com/jungle. The only way I know how to accomplish this is for the server to send a 3xx status code with the jungle URL, and the browser issues a new request to retrieve it.

Is it possible for the server to deliver the page jungle in response to the lion request, and just tell the browser "Set your URL to http://www.mysite.com/jungle"?

Upvotes: 0

Views: 1659

Answers (2)

Dan Grossman
Dan Grossman

Reputation: 52372

Yes, it's called URL rewriting. Both Apache and IIS have modules that allow this.

For Apache, in a .htaccess file or your httpd.conf within a virtual host:

RewriteEngine On
RewriteRule lion jungle [L]

The contents of jungle will be sent when lion is requested, without a redirect.

Upvotes: 2

Jonathan Wood
Jonathan Wood

Reputation: 67193

In ASP.NET, you can use Server.Transfer(). This causes a different page to be returned for the current request.

Note that the browser has no knowledge this is happening. It would still show http://www.mysite.com/lion in the address bar even though you would see the content from http://www.mysite.com/jungle.

If you want to inform the browser that a different page is being loaded, then you need a redirect as far as I'm concerned.

Upvotes: 1

Related Questions