Home arrow PHP arrow Page 2 - How to Redirect URLs in PHP

Permanent URL 301 Redirect in PHP - PHP

In this programming tutorial, we will look at different methods to redirect URLs in PHP. Specifically, we will be examining 301 URL redirect methods via code, meta refresh and the use of PHP header location settings to invoke a redirection.

TABLE OF CONTENTS:
  1. How to Redirect URLs in PHP
  2. Permanent URL 301 Redirect in PHP
  3. Refresh Redirects in PHP
By: Codex-M
Rating: starstarstarstarstar / 3
April 27, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

So far the examples above will return a 302 as the header status. The most important redirects in real-world implementations use the 301 redirection status. The primary reason is that it is the only “search engine friendly” redirect. If you moved the URL to its new location, search engine ranking scores such as Google PageRank won't be passed to the new URL unless you are using 301 redirects.

So how you will be able to 301 redirect using the PHP Header()? This time you need to use the http response code parameter of header(). Supposing you will 301 redirect to: http://localhost/targetpage.php from http://localhost/originatingpage.php

The redirection code will be:

<?php
header('Location:
http://localhost/targetpage.php',TRUE,301);
?>

The 301 is now included to make sure it will return a 301 header status. You can check this using Firebug if you are using a development server. Or if you are testing this using a live server, you might need to use a server header status checker such as: http://www.seoconsultants.com/tools/headers.

Common 301 redirection PHP code examples (always remember to place these codes above any HTML and ensure that HTML is not outputted first):

1.) Conditional 301 redirect (redirect when the URL matches to a given URL)

<?php

//Function to get the current URL of the website
function currentpageurl() {
 $currentpageurl = 'http';
 if ($_SERVER["HTTPS"] == "on") {$currentpageurl .= "s";}
 $currentpageurl .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $currentpageurlL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $currentpageurl .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $currentpageurl;
}

//Equate the current URL with $currentpageurl variable
$currentpageurl=currentpageurl();

//Conditional check to redirect
if ($currentpageurl=="
http://www.example.org/301redirecttest.php") {

//URL match, do 301 redirect
header('Location:
http://www.example.org/',TRUE,301);
exit();
}
?>

The above example will only do a 301 redirect to the homepage URL http://www.example.org if the URL is http://www.example.org/301redirecttest.php

2.) 301 redirect all non-www URLs to www URLs using PHP (assuming your protocol is http:// all throughout your website and not using https):

<?php

//Get current page URL
$currentpageurl= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

//Check if the URL is non-www version
if (!(preg_match("/www/i",$currentpageurl))) {

//URL is a non-www version, append www to the URL
$wwwversion="
http://www.".$currentpageurl;

//301 redirect to the www version
header("Location: $wwwversion",TRUE,301);
exit();
}
?>

3.) 301 redirect all http to https (non-secure to secure 301 redirection):

<?php

$currenturl= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

//Check if it is not using the secure port which is 443
if ($_SERVER["SERVER_PORT"] != "443") {

//not connecting through secure https port
, this URL is using http protocol

//append https
$secureversion="
https://".$currenturl;

//Redirect to the secure version
header("Location: $secureversion",TRUE,301);
exit();
}

echo "Hi, this will redirect any non-secure page to equivalent secure page";
?>

Disclaimer and remarks: The above scripts are provided for illustration/basic example purposes only. In actual implementation, the above sample redirection scripts might be tweaked further to fully fit the implementation.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: