htaccess Elite

.htaccess tutorial


All times are UTC [ DST ]





Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Redirects with php
PostPosted: 04 Dec 2006 12:49 
Offline

Joined: 28 Oct 2006 07:37
Posts: 44
To forward http://www.site.com to site.com:
Code:
<?php
if(stristr($_SERVER["HTTP_HOST"], 'www')){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://site.com/" . $_SERVER["REQUEST_URI"]);
  die();
  }
?>


To forward site.com to http://www.site.com, use the following code:
Code:
<?php
if(!stristr($_SERVER["HTTP_HOST"], 'www')){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://www.site.com/" . $_SERVER["REQUEST_URI"]);
  exit();
  }
?>



* Notice how we send the HTTP 301 header and then we immediately follow it with the new location.
* Also, notice that we forward the REQUEST_URI value. The REQUEST_URI is the path within the website to the web page. For example, in http://ekstreme.com/phpcounter/index.php, the /phpcounter/index.php part is the REQUEST_URI. Our forwarding code above forwards to the user to the page they requested.
* Notice the exit() statement: this makes sure that the PHP code following this code does not get executed and that only the forwarding information is sent to the browser.

This code is best used for single pages that require forwarding, and not whole websites. To use it, just add the code at the very beginning of the page's PHP file. Make sure there are no spaces or any other characters before the opening <?php, otherwise, the code won't work and you'll get a warning.


Top
 Profile  
 
 Post subject:
PostPosted: 01 Sep 2007 04:10 
Offline

Joined: 01 Sep 2007 03:06
Posts: 1
Thanks for the info. My add-in here....

Code:
<?php header("Location: http://www.yourtargetdomain.com/");?>
<p>If you're not redirected to the proper page in 5 seconds,
<a href="http://www.yourtargetdomain.com/" mce_href="http://www.yourtargetdomain.com/" target="_top"><strong>Click
Here</strong></a>.
</p>



Create a txt file and paste the code above to the txt file. Name this file as index.php and paste this file to a specified folder, say:
ftp://root/www.yourdomain.com/folder

Create a link to http://www.yourdomain.com/folder/index.php. When user click on http://www.yourdomain.com/folder/index.php, he will be redirected to http://www.yourtargetdomain.com/

Cheers,
Alan


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB