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

Git Ignore All Files Except PHP etc + Solution
PHP 5.3 Is Released
Netbeans 6.7 is Out. Yay :-)
RGBA Cross Browser Support + Solution

Most Popular Posts

Magento Developer UK Freelance osCommerce UK Magento UK Developers on Linked.in CRE Loaded UK

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

Possibly Relevant Posts:

  • no matches

Feed | Respond | Trackback

Leave a Reply

  • RSS Feed
  • Categories

    • apache
    • barcode
    • business
    • creloaded
    • css
    • curl
    • customer services
    • debugging
    • eclipse
    • ecommerce
    • edmondscommerce
    • email
    • excel
    • firefox
    • flash
    • gd
    • git
    • graphs
    • hosting
    • icecat
    • internet news
    • javascript
    • link building
    • linux
    • mac
    • magento
    • management
    • misc
    • mod_rewrite
    • mysql
    • open suse
    • oscommerce
    • php
    • plesk
    • portfolio
    • product catalogue
    • product feed
    • programming
    • regular expressions
    • ria
    • scraping
    • search engine optimisation
    • security
    • seo
    • spidering
    • twitter
    • ubuntu
    • Uncategorized
    • usability
    • vps
    • web design
    • web development
    • Windows
    • xampp
    • zend framework
    • zip
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
  • Tags

    alpha browser case creloaded css curl development directories filezilla firefox flush google googlecheckout hosts file html internet explorer jaunty links linux magento migration mod_security mysql myths oscommerce php plesk reciprocal linking rgba search engine optimisation seo spidering spotify ssl synchronisation table transparency ubuntu uk virtualbox web web design xml zend form zend framework
  • Random Posts

    • Faster File Browsing with Tabs for Ubuntu
    • Grub Error 21 on a Laptop + Solution
    • CRE Loaded PCI Compliance Validated
    • Ubuntu Force Quit
    • Eclipse Navigation Shortcuts
    • Optimised Magento AMI for Amazon EC2 - Host Magento on the Cloud
    • Apache, mod_rewrite and SEO
    • Setting Up Amazon's AMTU on a Linux Server
    • Colorzilla for Ubuntu Alternative - Gcolor2
    • Xdebug Display Full Details on var_dump()
  • Most Popular Posts

    • MySQL Copy Table from One Database to Another (15)
    • PHP Email Attachment Function (10)
    • PHP Save Images Using cURL (6)
    • PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode (6)
    • Magento Sites Go Down if Magentocommerce.com Goes Down (6)
  • Recent Comments

    • Doru on Setting up a 64bit Linux PHP Development Enviroment
    • admin on Migrate Magento to Alternative Server
    • jon on Migrate Magento to Alternative Server
    • admin on Magento Sites Go Down if Magentocommerce.com Goes Down
    • Sam on Magento Sites Go Down if Magentocommerce.com Goes Down

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

Freelance PHP Web Design UK Commercial Web Design