In a standard OpenCart installation the robots.txt file is static and the same for all stores within a multi-store. This means that the second and third store, etc. on separate domains give the robot a Sitemap with a foreign domain, and the Host directive for Yandex points to the wrong site. This script replaces a static file with a dynamic one: with each request, it itself determines the current domain and generates the correct robots.txt specifically for this store.
The main advantage of is support for multi-stores without complex settings and module installations.
One robots.php file is located at the root of the server and serves all stores simultaneously. Each domain receives its own response:
Example:
# shop-one.ru/robots.txt
Host: shop-one.ru
Sitemap:https://shop-one.ru/sitemap.xml
# shop-two.ru/robots.txt
Host: shop-two.ru
Sitemap: https://shop-two.ru/sitemap.xml
No manual edits for each domain - the script determines everything itself from the request header.
Basic set of rules (out of the box)
The script already contains closed routes, typical for any store on OpenCart:
Personal account, ordering, affiliate program
Product comparison and search page
System directories: /system, /download, /storage
URL with parameters: ?sort=, ?order=, ?limit=, ?filter=, ?tag=, ?tracking= and their & variants
Explicit static resolution: CSS, JS, images, fonts
How to add your own directive:
All rules are stored in the $common_rules array.
To add the one you needban, insert the line into the array:
// Close the manufacturers page
$common_rules = array(
...
'Disallow: /*route=product/manufacturer',
...
);
To add pages only for a specific store, use $host_only:
// Close the special section only on the second store
if ($host_only === 'shop-two.ru') {
$lines[] = 'Disallow: /wholesale/';
}
How to remove an unnecessary directive:
Find the line in the $common_rules array and delete it. For example, if you don't need to filter by name:
// Was - remove these two lines:
'Disallow: /*?filter_name=',
'Disallow: /*&filter_name=',
How to add rules only for Yandex:
The block for Yandex is formed separately after the general one.
Add your directives before the Host directive -
// Additionally close the promotions page from Yandex
$lines[] = 'User-agent: Yandex';
$lines = array_merge($lines, $common_rules);
$lines[] = 'Disallow: /promo/'; // ← add here
if ($host_only) {
$lines[] = 'Host: ' . $host_only;
}
Installation:
Download and extract the multistore robots sitemap.zip archive
Upload robots.php to the root of the site via FTP
Rename the existing robots.txt, for example adding an underscore robots.txt_ to it at the end (this will disable this file).
Add a rulein .htaccess to the main OpenCart block:
Open the htaccess file, find the line RewriteRule ^system/storage/(.*) index.php?route=error/not_found [L]
Add below
RewriteRule ^robots\.txt$ robots.php [L]
Check the result:
Open https://your-domain/robots.txt in a browser or through Yandex.Webmaster tools.
And then the correspondingly operating sub-stores
https:// your-domain-2.ru /robots.txt
etc.
Each robot must now point to a separate sitemap, host, etc.
Compatibility
OpenCart 3.x / OcStore 3.x / LiveStore 3.x
PHP 7.0+
Apache with mod_rewrite
Works behind Nginx, Cloudflare and any reverse proxy with X-Forwarded-Proto header
Access to the databaseno data or admin panel required
Sitemap:
By default, in Opencart, the sitemap module included in the assembly generates its own sitemap.xml for each multi-store.
For this reason, it does not need to be modified for these purposes.
If you use third-party sitemap generation modules, simply replace in the line
$sitemap_url = $base_url . '/sitemap.xml';
to your own values
For coffee!