Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robots.txt with multilang/multishop #12106

Merged
merged 4 commits into from Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
142 changes: 95 additions & 47 deletions classes/Tools.php
Expand Up @@ -2321,6 +2321,52 @@ public static function getMediaServer($filename)
return Tools::usingSecureMode() ? Tools::getShopDomainSsl() : Tools::getShopDomain();
}

/**
* Get domains information with physical and virtual paths
*
* e.g: [
* prestashop.localhost => [
* physical => "/",
* virtual => "",
* id_shop => "1",
* ]
* ]
*
* @return array
*/
public static function getDomains()
{
$domains = [];
foreach (ShopUrl::getShopUrls() as $shop_url) {
/** @var ShopUrl $shop_url */
if (!isset($domains[$shop_url->domain])) {
$domains[$shop_url->domain] = [];
}

$domains[$shop_url->domain][] = [
'physical' => $shop_url->physical_uri,
'virtual' => $shop_url->virtual_uri,
'id_shop' => $shop_url->id_shop,
];

if ($shop_url->domain == $shop_url->domain_ssl) {
continue;
}

if (!isset($domains[$shop_url->domain_ssl])) {
$domains[$shop_url->domain_ssl] = [];
}

$domains[$shop_url->domain_ssl][] = [
'physical' => $shop_url->physical_uri,
'virtual' => $shop_url->virtual_uri,
'id_shop' => $shop_url->id_shop,
];
}

return $domains;
}

public static function generateHtaccess($path = null, $rewrite_settings = null, $cache_control = null, $specific = '', $disable_multiviews = null, $medias = false, $disable_modsec = null)
{
if (defined('_PS_IN_TEST_')
Expand Down Expand Up @@ -2370,33 +2416,7 @@ public static function generateHtaccess($path = null, $rewrite_settings = null,
fwrite($write_fd, trim($specific_before) . "\n\n");
}

$domains = array();
foreach (ShopUrl::getShopUrls() as $shop_url) {
/** @var ShopUrl $shop_url */
if (!isset($domains[$shop_url->domain])) {
$domains[$shop_url->domain] = array();
}

$domains[$shop_url->domain][] = array(
'physical' => $shop_url->physical_uri,
'virtual' => $shop_url->virtual_uri,
'id_shop' => $shop_url->id_shop,
);

if ($shop_url->domain == $shop_url->domain_ssl) {
continue;
}

if (!isset($domains[$shop_url->domain_ssl])) {
$domains[$shop_url->domain_ssl] = array();
}

$domains[$shop_url->domain_ssl][] = array(
'physical' => $shop_url->physical_uri,
'virtual' => $shop_url->virtual_uri,
'id_shop' => $shop_url->id_shop,
);
}
$domains = self::getDomains();

// Write data in .htaccess file
fwrite($write_fd, "# ~~start~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again\n");
Expand Down Expand Up @@ -2437,7 +2457,7 @@ public static function generateHtaccess($path = null, $rewrite_settings = null,
foreach ($medias as $media) {
foreach ($media as $media_url) {
if ($media_url) {
$media_domains .= 'RewriteCond %{HTTP_HOST} ^' . $media_url . '$ [OR]' . "\n";
$media_domains .= 'RewriteCond %{HTTP_HOST} ^' . $media_url . '$ [OR]' . PHP_EOL;
}
}
}
Expand All @@ -2447,13 +2467,12 @@ public static function generateHtaccess($path = null, $rewrite_settings = null,
}

foreach ($domains as $domain => $list_uri) {
$physicals = array();
foreach ($list_uri as $uri) {
fwrite($write_fd, PHP_EOL . PHP_EOL . '#Domain: ' . $domain . PHP_EOL);
if (Shop::isFeatureActive()) {
fwrite($write_fd, 'RewriteCond %{HTTP_HOST} ^' . $domain . '$' . "\n");
fwrite($write_fd, 'RewriteCond %{HTTP_HOST} ^' . $domain . '$' . PHP_EOL);
}
fwrite($write_fd, 'RewriteRule . - [E=REWRITEBASE:' . $uri['physical'] . ']' . "\n");
fwrite($write_fd, 'RewriteRule . - [E=REWRITEBASE:' . $uri['physical'] . ']' . PHP_EOL);

// Webservice
fwrite($write_fd, 'RewriteRule ^api$ api/ [L]' . "\n\n");
Quetzacoalt91 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -2463,7 +2482,7 @@ public static function generateHtaccess($path = null, $rewrite_settings = null,
$rewrite_settings = (int) Configuration::get('PS_REWRITING_SETTINGS', null, null, (int) $uri['id_shop']);
}

$domain_rewrite_cond = 'RewriteCond %{HTTP_HOST} ^' . $domain . '$' . "\n";
$domain_rewrite_cond = 'RewriteCond %{HTTP_HOST} ^' . $domain . '$' . PHP_EOL;
// Rewrite virtual multishop uri
if ($uri['virtual']) {
if (!$rewrite_settings) {
Expand All @@ -2486,10 +2505,10 @@ public static function generateHtaccess($path = null, $rewrite_settings = null,
if (Configuration::get('PS_LEGACY_IMAGES')) {
fwrite($write_fd, $media_domains);
fwrite($write_fd, $domain_rewrite_cond);
fwrite($write_fd, 'RewriteRule ^([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1-$2$3$4.jpg [L]' . "\n");
fwrite($write_fd, 'RewriteRule ^([a-z0-9]+)\-([a-z0-9]+)(\-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1-$2$3$4.jpg [L]' . PHP_EOL);
fwrite($write_fd, $media_domains);
fwrite($write_fd, $domain_rewrite_cond);
fwrite($write_fd, 'RewriteRule ^([0-9]+)\-([0-9]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1-$2$3.jpg [L]' . "\n");
fwrite($write_fd, 'RewriteRule ^([0-9]+)\-([0-9]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1-$2$3.jpg [L]' . PHP_EOL);
}

// Rewrite product images < 100 millions
Expand All @@ -2506,17 +2525,17 @@ public static function generateHtaccess($path = null, $rewrite_settings = null,
}
fwrite($write_fd, $media_domains);
fwrite($write_fd, $domain_rewrite_cond);
fwrite($write_fd, 'RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]' . "\n");
fwrite($write_fd, 'RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]' . PHP_EOL);
fwrite($write_fd, $media_domains);
fwrite($write_fd, $domain_rewrite_cond);
fwrite($write_fd, 'RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]' . "\n");
fwrite($write_fd, 'RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]' . PHP_EOL);
}

fwrite($write_fd, "# AlphaImageLoader for IE and fancybox\n");
if (Shop::isFeatureActive()) {
fwrite($write_fd, $domain_rewrite_cond);
}
fwrite($write_fd, 'RewriteRule ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]' . "\n");
fwrite($write_fd, 'RewriteRule ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]' . PHP_EOL);
}
// Redirections to dispatcher
if ($rewrite_settings) {
Expand Down Expand Up @@ -2615,6 +2634,7 @@ public static function generateRobotsFile($executeHook = false)
}

$robots_content = static::getRobotsContent();
$languagesIsoIds = Language::getIsoIds();

if (true === $executeHook) {
Hook::exec('actionAdminMetaBeforeWriteRobotsFile', array(
Expand All @@ -2639,36 +2659,64 @@ public static function generateRobotsFile($executeHook = false)
if (count($robots_content['Allow'])) {
fwrite($write_fd, "# Allow Directives\n");
foreach ($robots_content['Allow'] as $allow) {
fwrite($write_fd, 'Allow: ' . $allow . "\n");
fwrite($write_fd, 'Allow: ' . $allow . PHP_EOL);
}
}

// Private pages
if (count($robots_content['GB'])) {
fwrite($write_fd, "# Private pages\n");
foreach ($robots_content['GB'] as $gb) {
fwrite($write_fd, 'Disallow: /*' . $gb . "\n");
fwrite($write_fd, 'Disallow: /*' . $gb . PHP_EOL);
}
}

// Directories
if (count($robots_content['Directories'])) {
fwrite($write_fd, "# Directories\n");
foreach ($robots_content['Directories'] as $dir) {
fwrite($write_fd, 'Disallow: /' . $dir . "\n");
foreach (self::getDomains() as $domain => $uriList) {
fwrite(
$write_fd,
sprintf(
'# Directories for %s%s',
$domain,
PHP_EOL
)
);
// Disallow multishop directories
foreach ($uriList as $uri) {
foreach ($robots_content['Directories'] as $dir) {
fwrite($write_fd, 'Disallow: ' . $uri['physical'] . $dir . PHP_EOL);
}
}

// Disallow multilang directories
if (!empty($languagesIsoIds)) {
foreach ($languagesIsoIds as $language) {
foreach ($robots_content['Directories'] as $dir) {
fwrite(
$write_fd,
sprintf(
'Disallow: /%s/%s%s',
$language['iso_code'],
$dir,
PHP_EOL
)
);
}
}
}
}
}

// Files
if (count($robots_content['Files'])) {
$language_ids = Language::getIDs();
fwrite($write_fd, "# Files\n");
foreach ($robots_content['Files'] as $iso_code => $files) {
foreach ($files as $file) {
if (!empty($language_ids) && count($language_ids) > 1) {
fwrite($write_fd, 'Disallow: /*' . $iso_code . '/' . $file . "\n");
if (!empty($languagesIsoIds)) {
fwrite($write_fd, 'Disallow: /*' . $iso_code . '/' . $file . PHP_EOL);
} else {
fwrite($write_fd, 'Disallow: /' . $file . "\n");
fwrite($write_fd, 'Disallow: /' . $file . PHP_EOL);
}
}
}
Expand All @@ -2685,7 +2733,7 @@ public static function generateRobotsFile($executeHook = false)
fwrite($write_fd, "# Sitemap\n");
$sitemap_filename = basename($sitemap_file);
fwrite($write_fd, 'Sitemap: ' . (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://') . $_SERVER['SERVER_NAME']
. __PS_BASE_URI__ . $sitemap_filename . "\n");
. __PS_BASE_URI__ . $sitemap_filename . PHP_EOL);
}

if (true === $executeHook) {
Expand Down
1 change: 1 addition & 0 deletions controllers/admin/AdminShopUrlController.php
Expand Up @@ -466,6 +466,7 @@ public function processSave()
$return = parent::processSave();
if (!$this->errors) {
Tools::generateHtaccess();
Tools::generateRobotsFile();
Tools::clearSmartyCache();
Media::clearCache();
}
Expand Down