PH Ranking - Online Knowledge Base - 2025-09-06

How to Dynamically Generate robots.txt Files Using PHP

To dynamically generate robots.txt files using PHP, you create a PHP script (e.g., robots.php) that outputs the robots.txt content as plain text and configure your web server to serve this script whenever robots.txt is requested. This allows you to customize the robots.txt content based on conditions like domain, environment, or user-agent.

Here is a step-by-step approach:

  1. Create a PHP script (robots.php):
<?php
header('Content-Type: text/plain');

// Example: Customize based on domain
if ($_SERVER['HTTP_HOST'] == 'test.example.com') {
    echo "User-agent: *\nDisallow: /"; // Block all on test site
} else {
    echo "User-agent: *\nDisallow: /private/\nAllow: /public/\nSitemap: https://example.com/sitemap.xml";
}
?>
  1. Configure your web server to rewrite requests for robots.txt to robots.php:
  • For Apache, add this rule to your .htaccess file:
RewriteEngine On
RewriteRule ^robots\.txt$ /robots.php [L]

This makes requests to robots.txt serve the dynamic PHP script instead of a static file.

  1. Benefits:
  • You can generate different robots.txt content dynamically based on domain, environment (test/live), or other logic.
  • Avoids manual updates and errors from uploading wrong robots.txt files.
  • Allows conditional blocking or allowing of user-agents or paths.
  1. Additional tips:
  • Always set the content type header to text/plain to ensure proper interpretation by crawlers.
  • You can read from a template file and modify it dynamically if preferred.
  • Keep the output size reasonable (under 500 KB) for crawler compatibility.

This method is widely used to manage robots.txt dynamically for multi-domain or multi-environment websites using PHP and Apache rewrite rules.

Internet images

Ang PH Ranking ay nag-aalok ng pinakamataas na kalidad ng mga serbisyo sa website traffic sa Pilipinas. Nagbibigay kami ng iba’t ibang uri ng serbisyo sa trapiko para sa aming mga kliyente, kabilang ang website traffic, desktop traffic, mobile traffic, Google traffic, search traffic, eCommerce traffic, YouTube traffic, at TikTok traffic. Ang aming website ay may 100% kasiyahan ng customer, kaya maaari kang bumili ng malaking dami ng SEO traffic online nang may kumpiyansa. Sa halagang 720 PHP bawat buwan, maaari mong agad pataasin ang trapiko sa website, pagandahin ang SEO performance, at pataasin ang iyong mga benta!

Nahihirapan bang pumili ng traffic package? Makipag-ugnayan sa amin, at tutulungan ka ng aming staff.

Libreng Konsultasyon

Free consultation Customer support

Need help choosing a plan? Please fill out the form on the right and we will get back to you!

Fill the
form