Re-direction without flickering

J

Jens Lenge

When I set a re-direction to a different site via

<meta http-equiv="refresh" content="0; URL=http://another-domain.com">

in the header, my current page will be displayed shortly before the
redirection takes effect, causing an ugly flickering (tested with FF and
IE6). How can I set the re-direction so that it takes effect *before* the
current page is displayed?

I would like the new page to be shown directly (without flickering), and the
current page be shown only if the re-direction did not work (due to browser
settings or whatever).

Any ideas?

Cheers, Jens
 
H

humbads

If you have scripting support available, use the HTTP 301 response
code. This far better for search engines too, because then they will
automatically pick up the new location of the page.

In ASP-VBScript:

<%@ Language=VBScript %>
<%
' Permanent redirection to anothersite.com
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www.somacon.com/blog/"
Response.End
%>

In PHP:

<?php
// Permanent redirection to anothersite.com
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.somacon.com/blog/");
exit();
?>
 
A

Andy Dingley

When I set a re-direction to a different site via

<meta http-equiv="refresh" content="0; URL=http://another-domain.com">

in the header, my current page will be displayed shortly before the
redirection takes effect,

There are three ways to do redirection.

One is at the server, inside the initial HTTP response. This could
use the ASP Server.Transfer() method. It's not strictly redirection,
the whole response gets switched into serving content from somewhere
else. This is often a good way to do things, if you're "Serving
something from somewhere it isn't obviously at". The client sees
nothing here - they just get content as if it had arrived from the
first URL.

The second is "classic HTTP redirection" The server sends a HTTP 301
response (sometimes a 302) and the client then performs the
redirection themselves. You can do this with the ASP
Server.Redirect() method or for static HTML by configuring .htaccess
The client may (but probably won't) display much when it follows one
of these redirections.

The third is using a http-equiv meta inside a HTML page. This is a
poor emulation of the second method. It's performed at the client, but
the client must first obtain the HTML page (which can be slow) and
parse it. Most clients will also make some attempt at rendering the
page before following the redirection, and this is what make the
visible flash.


So in most cases, do it the second way. Serve a 301 if it's
permanent, 302 if it's temporary. Google will give you .htaccess
tutorials.
 
J

Jens Lenge

humbads & Andy,

Thanks for your comments. From what I learned, it seems that I'll have to
provide some more details to see which of the suggested methods would be
best for a case like mine. First, I do have PHP scripting available, and
also some PHP knowledge. (And I would like to get along with HTML and PHP as
I have no knowledge about ASP.)

What I'd like to do on the homepage is:

1. Use a PHP function to determine the language of the browser.

2. If the language could be successfully retrieved, automatically forward to
the specific homepage for that language.

3. If the language could not be retrieved, or if I have no specific homepage
for that language, or if the redirection failed due to browser security
settings, a multi-language "default" homepage is displayed, where the user
may select his language himself.

For step 2, I currently use the meta-tag listed in my OP (which results in a
short flickering es the multi-language page is shortly displayed before the
language-specific page appears). As far as I learned from your postings, I
could use PHP to send 301/302 codes instead. I assume if I do this before
the HTML part of the document, the redirection would take place without
displaying the page, so it would not flicker. Right?

However, is it appropriate to use 301/302 codes in this case? After all, I
do not want to indicate that the site has actually moved temporarily or
permanently, but only to forward to a specific sub-page within the same
site.

What do you think?

Jens
 
A

Andy Dingley

I assume if I do this before
the HTML part of the document, the redirection would take place without
displaying the page, so it would not flicker. Right?
Yes.

What do you think?

Send a 302
 
H

humbads

Yes, send 302 and it won't flicker when you use the Location header.
For public websites, it's good to use the appropriate status code
whenever doing a redirect. For intranet applications or private
websites, I typically do not bother with sending a status code; I just
do the redirect by itself.
 
J

Jens Lenge

As far as I googled I can set a 302 redirection by simply setting

header("Location: http://www.domain1.com/english/");

I tried this and found that it works on my local test server (XAMPP/Apache
on Windows XP), but it does *not* work at my providers' webspace (Strato,
using Apache on Unix).

However, I *can* successfully use a similar call for redirecting to the
*same* page. I have 2 domains that target the same site, and I use the
following code to make the browser display the name of domain2 also when it
is called with domain1:


if ($_SERVER['SERVER_NAME'] != 'www.domain2.com')
{
header("Location: http://www.domain2.com");
exit();
}


What could be the reason why the above works perfectly at my providers'
webspace, but I cannot redirect to a different page? I guess its a matter of
configuration as it does work for my local server.

Clueless...
Jens
 
J

Jens Lenge

Gaaaaa... just found the solution: I used the header("Location: ...")
redirection *after* the <html> tag.

Now I no longer wonder why it did not work at the providers' webspace, but
rather wonder why it did for my local server. However, now it runs smoothly
on both servers.

Thank again for all your advice!

Jens
 
H

humbads

Don't worry, that is a very common mistake. I still make it sometimes
after so many years.

But you bring up a good point, when I use the Location header by itself
in PHP/Apache, I get these headers.

HTTP/1.1·302·Found
Server:·Apache/1.3.33·(Debian·GNU/Linux)
Location:·http://www.somacon.com/

Using Response.Redirect in ASP/IIS results in:

HTTP/1.1·302·Object·moved
Server:·Microsoft-IIS/5.1
Location:·http://www.somacon.com/blog/

If I use Response.AddHeader "Location" by itself in ASP/IIS, I get:

HTTP/1.1·200·OK
Server:·Microsoft-IIS/5.1
Location:·http://www.somacon.com/blog/

Something, either the web server or the scripting environment, is
automatically changing the status code when the location header is
specified. Anyone know what it is? I'm guessing it is the scripting
environment.

Location by itself will always redirect. But I guess to be safe, one
would want to specify the status code where it needs to be explicit,
rather than relying on the scripting environment/web server to
automatically set it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top