Re-direction without flickering

Discussion in 'HTML' started by Jens Lenge, Apr 7, 2005.

  1. Jens Lenge

    Jens Lenge Guest

    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
     
    Jens Lenge, Apr 7, 2005
    #1
    1. Advertising

  2. Jens Lenge

    humbads Guest

    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();
    ?>
     
    humbads, Apr 7, 2005
    #2
    1. Advertising

  3. Jens Lenge

    Andy Dingley Guest

    On Thu, 7 Apr 2005 19:27:07 +0200, "Jens Lenge" <>
    wrote:

    >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.
     
    Andy Dingley, Apr 7, 2005
    #3
  4. Jens Lenge

    humbads Guest

    humbads, Apr 7, 2005
    #4
  5. Jens Lenge

    Jens Lenge Guest

    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
     
    Jens Lenge, Apr 7, 2005
    #5
  6. Jens Lenge

    Andy Dingley Guest

    On Thu, 7 Apr 2005 23:35:47 +0200, "Jens Lenge" <>
    wrote:

    >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
     
    Andy Dingley, Apr 8, 2005
    #6
  7. Jens Lenge

    humbads Guest

    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.
     
    humbads, Apr 8, 2005
    #7
  8. Jens Lenge

    Jens Lenge Guest

    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
     
    Jens Lenge, Apr 8, 2005
    #8
  9. Jens Lenge

    Jens Lenge Guest

    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
     
    Jens Lenge, Apr 8, 2005
    #9
  10. Jens Lenge

    humbads Guest

    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.
     
    humbads, Apr 8, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SEWilson

    Re: Flickering effect

    SEWilson, Aug 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,088
    SEWilson
    Aug 23, 2003
  2. yop

    Flickering on Listbox control

    yop, Aug 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    442
  3. vinay

    Flickering

    vinay, Aug 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    523
    S. Justin Gengo
    Aug 27, 2003
  4. Georg Andersson

    repaint without flickering

    Georg Andersson, May 17, 2004, in forum: Java
    Replies:
    6
    Views:
    7,035
    Silvio Bierman
    May 17, 2004
  5. dhavalshah
    Replies:
    0
    Views:
    382
    dhavalshah
    Dec 26, 2008
Loading...

Share This Page