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

Magento Most Popular Extensions
Google Chrome for Linux Beta
Firefox Address Bar Lag + Solution
Custom Buttons for Firefox

Most Popular Posts

Magento Developer UK Freelance osCommerce UK Magento Training 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

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

    • February 2010
    • January 2010
    • December 2009
    • November 2009
    • October 2009
    • September 2009
    • August 2009
    • 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

    bulk course cre loaded creloaded css custom developer development directories drupal error find firefox git google hosts file html jaunty javascript leeds links linux magento mysql netbeans oscommerce performance php plesk ppc problem replace search engine optimisation seo server symfony table training ubuntu uk virtualbox web web design xml zend framework
  • Random Posts

    • Cookie Based 302 Redirector
    • Why VPS is Not for Everyone - Yet
    • Douglas Crockford Javascript Videos Available
    • Ecommerce Back Office Optimisations
    • Google Analytics Scan
    • Ultimate osCommerce Checkout - Fast and Friendly
    • Magento Template Path Hints Tutorial
    • MySQL Dump Partial Restore - Split MySQL Dump into Tables
    • Selenium Web Application Testing Platform
    • Hardware Information in Ubuntu
  • Recent Comments

    • admin on Magento Backup Error Filesystem.php on line 234 + Solution
    • admin on Magento Leeds
    • Matthew Dolley on Magento Leeds
    • kash on PHP Email Attachment Function
    • Hussein on PHP Save Images Using cURL
  • Category Specific RSS

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

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

Freelance PHP Web Design UK Commercial Web Design