Edmonds Commerce Logo
  • home
    • blog
  • ecommerce
    • product catalogue
    • order processing
    • customer services
    • stock control
    • human resources
    • management information
  • development
    • oscommerce
    • php
    • mysql
    • open source
    • performance tuning
  • design
  • marketing
  • contact us
    • pricing

Edmonds Commerce Blog

Freelance PHP Ecommerce and SEO Developer in the UK

Latest Posts

CRELoaded Remove Google Ads -
ICECat Integration with osCommerce, Magento etc
Magento UK
PHP Cached Download Function

Most Popular Posts

PHP Email Attachment Function Freelance osCommerce UK Ultimate osCommerce Checkout - Fast and Friendly PHP : Dead Easy Excel Export

PHP 301 Redirect Function with Headers Check and Javascript Fallback

May 22nd, 2008
Read More php, search engine optimisation

If you are changing your URL structure, for example when moving to a search engine friendly URLs system, you need to be able to let Google and all the other search engines that the page has moved to the new URL. You really don't want to display the same content on two URLs

So to achieve this we can do what is called a 301 redirect. This is a special redirect message which basically says "has permanently moved to". By doing this, all of the search engine power, rank or whatever you want to call it will be directly transferred to the new URL.

However

Sometimes your script might spit out the headers earlier than you expect. If this happens then your site will fail completely to load with a fatal error along the lines of

warning: Cannot modify header information - headers already sent by (output started at ....

We really don't want this to happen, so what we can do is wrap the redirection in a headers_sent check and then fall back to a javascript redirect if headers have been sent for some reason. This is belt and braces logic.

Note script tags need spaces removing

PLAIN TEXT
PHP:
  1. function requrl_check($correct_url, $delay=3){
  2.     $request_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  3.     if($request_url != $correct_url){
  4.         if(!headers_sent()){ //going to assume that headers are not sent
  5.         header("HTTP/1.1 301 Moved Permanently");
  6.           header('location: ' . $correct_url . '');
  7.           exit();
  8.        }else{
  9.           echo "<h3>The Page $request_url has Moved</h3>
  10.          <a href=$correct_url>$correct_url</a>";
  11.           echo '<h3>Now Refreshing in ' . $delay . ' Seconds</h3>Refreshing to:<br>' . $correct_url . '<s c r i p t type="text/JavaScript"><!--
  12.         setTimeout("location.href = \'' . $correct_url . '\';",' . ($delay * 1000) . ');
  13.         --> </s c r i p t>';
  14.             exit();
  15.         }
  16.     }
  17. }

tidy :-)

Further Reading:
www.stuntdubl.com
www.intertwingly.ne
webgasm.actiononline.biz
www.techcounter.com
www.chiropractichomepage.com
123howtoguide.com
searchblog.tamar.com

Bookmark this Post
Add 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Del.icio.usAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to diggAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to FURLAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to blinklistAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to redditAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Feed Me LinksAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to TechnoratiAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Yahoo My WebAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to NewsvineAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SocializerAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Ma.gnoliaAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Stumble UponAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Google BookmarksAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to RawSugarAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SquidooAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SpurlAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to BlinkBitsAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to NetvouzAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to RojoAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to BlogmarksAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to ShadowsAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Co.mments
Add 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to ScuttleAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to BloglinesAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to TailrankAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SegnaloAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to OKnotizieAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to NetscapeAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Bookmark.itAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to AskAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SmarkingAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to LinkagogoAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to DeliriousAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SocialdustAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Live-MSNAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SlashDotAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SphinnAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to DiggitaAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to SeotribuAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to FaceBookAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to UpnewsAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to WikioAdd 'PHP 301 Redirect Function with Headers Check and Javascript Fallback' to Social Bookmarking Reloaded

Feed | Respond | Trackback

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • creloaded
    • curl
    • customer services
    • debugging
    • ecommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • magento
    • management
    • mod_rewrite
    • mysql
    • oscommerce
    • php
    • plesk
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • scraping
    • search engine optimisation
    • spidering
    • ubuntu
    • web design
    • web development
    • Windows
    • xampp
    • zip
  • Archives

    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    addons advanced adverts blackhat blocking css curl development directories find firefox google hosts file html javascript keywords links msn mysql myths operators oscommerce paid links paid placement performance php ppc reciprocal linking replace screen scraping security seo serp speed spider spidering tuning user friendly vista web web design web developer
  • Random Posts

    • Online Backend System
    • Magento UK
    • PHP : Dead Easy Excel Export
    • PHP MySQL Query Function with Easy Error Message
    • Web Developer Toolbar for Firefox
    • Handy Easy Javascript
    • osCommerce Contribution Released: Server Migration Synchronisation
    • Chemos SEO URLS for osCommerce
    • PHP and Zip Files
    • Saving a File or Webpage using PHP

Edmonds Commerce related questions? Send us a message or call us on 0844 357 0201.

Freelance PHP Web Design UK Commercial Web Design