Jump to content


URL Redirect


3 replies to this topic

#1 Kuulest

    Advanced Member

  • Members
  • PipPipPip
  • 326 posts

Posted 18 May 2002 - 12:24 PM


Recently I encountered a problem while trying to
use PHP header() function for URL redirect (topic
may be found http://talk.phptalk.com/index.php?board=5;...ay;threadid=392 ).

I decided to explain all methods I know conserning
handling URL redirect, so that you don't have to
challenge the wall with your head
(as it was in my case) while looking for solution.

Basicly there are three ways of solving this problem:
[*]1. HTTP Headers (by using header() function)
[*]2. HTML Tag
[*]3. JavaScript



1. HTTP Headers

   
<?PHP
$URL="http://www.somedomain.com/";
header ("Location: $URL");
?>
Notes:
Here is note taken directly from PHP manual:
[*] HTTP/1.1 requires an absolute URI as argument to Location: including the scheme, hostname
and absolute path, but some clients accept relative URIs.
You can usually use $HTTP_SERVER_VARS['HTTP_HOST'],$HTTP_SERVER_VARS['PHP_SELF'] and dirname()
to make an absolute URI from a relative one yourself:

header("Location: http://".$HTTP_SERVER_VARS['HTTP_HOST']
."/".dirname($HTTP_SERVER_VARS['PHP_SELF'])
."/".$relative_url);
[*]header() function must be called before any output, even empty lines are sent to browser
(if you didn't use ob_start() )


2. HTML Tag
   <html> 
  <head>
      <meta http-equiv="refresh" content="5; url=http://www.somedomain.com">
  </head>
      <body>
      </body>
  </html>
   
Number 5 in above snippet is amout of seconds after which user_agent would be redirected.
As is quite predictable it can be any positive integer.


3. JavaScript
     <?PHP
         $url = 'http://www.somedomain.com';
        echo '<!--<SCRIPT LANGUAGE="JavaScript">';
        echo 'location.href = "'.$url.'"; 
        echo '</SCRIPT>-->';
    ?>
     
The only drawback of the last method is that it (of course) doesn't work on js-disabled browsers.



That is all,
Any and every comment,sugestion,correction etc would be ignored :) ,
Stay Kuul,
Yours Kuulest


P.S.: JavaScript method was proposed by CK and I
once again want to thank him (I will torture myself :shot:
forever for not thinking of this, seemingly simple,
but nevertheless cool solution)

#2 Guest_uranusalien_*

  • Guests

Posted 06 August 2002 - 10:12 PM

Alternative javascript possibilities:


<script language=javascript>
var timeID = setTimeout("window.location = 'URL';", 5000);
</script>


URL is the url to go to
5000 is miliseconds before redirect

#3 Gordito

    Advanced Member

  • Members
  • PipPipPip
  • 60 posts

Posted 10 August 2002 - 11:18 PM

A foolproof way is to put the redirect into apache. Say your busy site has be redone and recently spidered and old links are still coming up on a search. You could do something like this in the config file. If you're on a shared host I'm pretty sure you can throw it in an .htaccess file too.

RedirectMatch /oldpage.php(.*)$ /newpage.php$1

#4 waelbeso

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 26 December 2005 - 04:03 AM

<?php
$goto = $_REQUEST['goto'];
header("Location: $goto");
?>


---------------------------------


<META HTTP-EQUIV="REFRESH" Content=0;URL="http://www.yoursite.com/">





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users