Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ n98-magerun2 varnish:tuned

# Get active themes
n98-magerun2 dev:theme:active

# Generate nginx maps for URL redirects
n98-magerun2 hypernode:maps-generate
```

## Commands available
Expand Down Expand Up @@ -92,6 +95,33 @@ improves performance.
There is no output on success. It is suitable to be set up into a `@monthly` cron, e.g.:

@monthly n98-magerun2 db:maintain:search-query --root-dir=/path/to/magento2

### `n98-magerun2 hypernode:maps-generate`

Generates nginx map files for URL redirects from Magento 2's url_rewrite table. This is useful for
creating efficient nginx redirect configurations instead of relying on Magento to handle redirects.

Example output:

```nginx
# Nginx map for URL redirects
# Generated by n98-magerun2 hypernode:maps-generate

# 301 redirects
map $request_uri $redirect_uri {
default "";
"/old-product.html" "/new-product.html";
"/old-category.html" "/new-category.html";
}
```

Options:
- `--store=<id>` - Generate maps for a specific store ID only
- `--type=<301|302>` - Filter by redirect type (301 for permanent, 302 for temporary)

You can save the output to a file and include it in your nginx configuration:

n98-magerun2 hypernode:maps-generate > /etc/nginx/maps/magento-redirects.conf

### TODO

Expand Down
1 change: 1 addition & 0 deletions n98-magerun2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ commands:
- GetPageSpeed\DeployLocaleActiveCommand
- GetPageSpeed\DevModuleListCommand
- GetPageSpeed\DbMaintainSearchQuery
- GetPageSpeed\MapsGenerateCommand
122 changes: 122 additions & 0 deletions src/GetPageSpeed/MapsGenerateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace GetPageSpeed;

use N98\Magento\Command\AbstractMagentoCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;

use Symfony\Component\Console\Input\InputOption;
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;

class MapsGenerateCommand extends AbstractMagentoCommand
{

protected function configure()
{
$this
->setName('hypernode:maps-generate')
->setDescription('Generate nginx map files for URL redirects')
->addOption(
'store',
null,
InputOption::VALUE_OPTIONAL,
'Store ID (default: all stores)'
)
->addOption(
'type',
null,
InputOption::VALUE_OPTIONAL,
'Redirect type (301 or 302, default: both)'
)
;
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->detectMagento($output);
if ($this->initMagento()) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

$tableName = $resource->getTableName('url_rewrite');

// Build WHERE clause
$whereConditions = ['redirect_type IN (301, 302)'];

$storeId = $input->getOption('store');
if ($storeId !== null) {
$whereConditions[] = sprintf('store_id = %d', (int)$storeId);
}

$redirectType = $input->getOption('type');
if ($redirectType !== null && in_array($redirectType, ['301', '302'])) {
$whereConditions = [sprintf('redirect_type = %d', (int)$redirectType)];
if ($storeId !== null) {
$whereConditions[] = sprintf('store_id = %d', (int)$storeId);
}
}

$where = implode(' AND ', $whereConditions);

$sql = sprintf(
"SELECT request_path, target_path, redirect_type, store_id FROM `%s` WHERE %s ORDER BY store_id, redirect_type",
$tableName,
$where
);

$redirects = $connection->fetchAll($sql);

if (empty($redirects)) {
$output->writeln('<info>No redirects found.</info>');
return Command::SUCCESS;
}

// Generate nginx map format
$output->writeln('# Nginx map for URL redirects');
$output->writeln('# Generated by n98-magerun2 hypernode:maps-generate');
$output->writeln('');

// Group by redirect type
$redirectsByType = [];
foreach ($redirects as $redirect) {
$type = $redirect['redirect_type'];
if (!isset($redirectsByType[$type])) {
$redirectsByType[$type] = [];
}
$redirectsByType[$type][] = $redirect;
}

foreach ($redirectsByType as $type => $typeRedirects) {
$output->writeln(sprintf('# %d redirects', $type));
$output->writeln('map $request_uri $redirect_uri {');
$output->writeln(' default "";');

foreach ($typeRedirects as $redirect) {
$requestPath = '/' . ltrim($redirect['request_path'], '/');
$targetPath = '/' . ltrim($redirect['target_path'], '/');

// Escape special characters if needed
$requestPath = addslashes($requestPath);
$targetPath = addslashes($targetPath);

$output->writeln(sprintf(' "%s" "%s";', $requestPath, $targetPath));
}

$output->writeln('}');
$output->writeln('');
}

return Command::SUCCESS;
} else {
return Command::FAILURE;
}
}
}