PEAR Articles

  Home arrow PEAR Articles arrow Page 4 - Managing robots.txt using PHP: Generat...
PEAR ARTICLES

Managing robots.txt using PHP: Generating Dynamic Syntax
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2010-07-14

    Table of Contents:
  • Managing robots.txt using PHP: Generating Dynamic Syntax
  • Robots.txt using PHP example
  • Creating the PHP file and the static syntax
  • Upload the complete myrobots.txt.php to the website's root directory
  • Revise .htaccess to rewrite myrobots.txt.php to robots.txt

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Managing robots.txt using PHP: Generating Dynamic Syntax - Upload the complete myrobots.txt.php to the website's root directory


    (Page 4 of 5 )

    The complete/working myrobots.txt.php script is shown below:

    <?php

    header("Content-Type: text/plain");

    $currentsyntax='User-agent: *

    Disallow: */trackback

    Disallow: */feed

    Disallow: /searchresultpages

    Disallow: /wp-

    Disallow: /*?

    Disallow: /xmlrpc.php

    Disallow: /blockedbyrobots.php

    Allow: /wp-content/uploads/scripts/PHP-Server-Array-Variables.php

    Disallow: /postpdfcreator.php

    Disallow: /search/

    Disallow: /search

    Disallow: /*.js$

    Disallow: /antibot.php

    Disallow: /ajaxwebform/captcha.php

    Disallow: /*.jpg$

    Disallow: /ajaxwebform/ajaxvalidate.php

    Disallow: /hiddentextexample.php

    Disallow: /searchresultpages/

    Allow: /index.php?page_id=123&pg=2

    Allow: /site-map/?pg=2';

    echo $currentsyntax;

    /////////////////////////////////////////////////////////////////////////////

    //DYNAMICALLY GENERATE ROBOTS.TXT SYNTAX FOR YEAR DIRECTORY//////////////////

    ////////START////////////////////////////////////////////////////////////////

    //connect to MySQL database

    $username = "xxxx";

    $password = "xxxxx";

    $hostname = "xxxx";

    $database = "xxxxx";

    $dbhandle = mysql_connect($hostname, $username, $password)

    or die("Unable to connect to MySQL");

    $selected = mysql_select_db($database,$dbhandle)

    or die("Could not select $database");

    //Retrieve latest post dates in wp_post column

    $latestpostdate = mysql_query("SELECT max(post_date) from `wp_posts`") or die(mysql_error());

    $rowlatestpostdate = mysql_fetch_array($latestpostdate) or die("Invalid query" . mysql_error());

    $latestpostdatedata = $rowlatestpostdate['max(post_date)'];

    //Retrive the 1st post date in wp_post column

    $firstpostdate = mysql_query("SELECT min(post_date) from `wp_posts`") or die(mysql_error());

    $rowfirstpostdate = mysql_fetch_array($firstpostdate) or die("Invalid query" . mysql_error());

    $firstpostdatedata = $rowfirstpostdate['min(post_date)'];

    //Extract the post year of both 1st and latest post

    $firstdashlatest = strpos($latestpostdatedata,"-");

    $countlatestyear=strlen($latestpostdatedata);

    $substrcountlatest= ($countlatestyear-$firstdashlatest)*(-1);

    $latestpostyear = substr($latestpostdatedata,0,$substrcountlatest);

    $firstdashfirst = strpos($firstpostdatedata,"-");

    $countfirstyear=strlen($firstpostdatedata);

    $substrcountfirst= ($countfirstyear-$firstdashfirst)*(-1);

    $firstpostyear = substr($firstpostdatedata,0,$substrcountfirst);

    $i=$firstpostyear;

    echo strip_tags(nl2br("rn# Start of dynamically generated robots.txt syntax"));

    while ($i<=$latestpostyear) {

    echo strip_tags(nl2br("rnDisallow: /".$i++."/"));

    }

    echo strip_tags(nl2br("rn# End of dynamically generated robots.txt syntax"));

    /////////////////////////////////////////////////////////////////////////////

    ////////END OF DYNAMIC ROBOTS.TXT SYNTAX GENERATION//////////////////////////

    /////////////////////////////////////////////////////////////////////////////

    echo strip_tags(nl2br("rn"));

    echo strip_tags(nl2br("rnSitemap: http://www.php-developer.org/sitemap.xml"));

    ?>

    More PEAR Articles Articles
    More By Codex-M

    blog comments powered by Disqus

    PEAR ARTICLES ARTICLES

    - Installing PEAR
    - PEAR: an Introduction
    - Managing robots.txt using PHP: Generating Dy...
    - Deleting Authors from a PEAR Content Managem...
    - PEAR CMS: Index and Delete Scripts
    - Listing Articles for a PEAR Content Manageme...
    - Building an Authors Page for a PEAR CMS
    - Building the View Details Page in a PEAR CMS
    - Creating the Main Pages of a PEAR CMS
    - Completing the Login Script for a PEAR CMS
    - User Authentication for a PEAR CMS
    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning


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