Problem/Motivation

It'd be useful to have a Drush command to purge circular redirects.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork redirect-3419718

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

DamienMcKenna created an issue. See original summary.

damienmckenna’s picture

This logic works for me, it could be turned into a command.

  /**
   * Purge circular redirects.
   *
   * @command mymodule:purge-circular-redirects
   */
  public function fixCircularRedirects() {
    $db = \Drupal::database();
    $records = $db->query("SELECT redirect.rid
      FROM {redirect}
      INNER JOIN {path_alias}
      	ON CONCAT('/', redirect.redirect_source__path) = path_alias.alias")
      ->fetchCol();
    if (!empty($records)) {
      $this->logger->notice($this->t('@count redirect records to purge: @list', [
        '@count' => count($records),
        '@list' => implode(', ', $records),
      ]));
      $db->delete('redirect')
        ->condition('rid', $records, 'IN')
        ->execute();
      $this->logger->notice($this->t('@count redirect records purged.', [
        '@count' => count($records),
      ]));
    }
    else {
      $this->logger->notice($this->t('No circiular redirects found.'));
    }
  }

uttam made their first commit to this issue’s fork.

uttam’s picture

Status: Active » Needs review
StatusFileSize
new43.75 KB
new3.31 MB

Hello everyone, I have created this Drupal Drush command which facilitates the management of circular redirects using Drush as mentioned in the issue. Firstly, It retrieves all redirect entities from the database using EntityTypeManagerInterface, constructs a map linking source URLs to their corresponding destinations, identifies circular redirect relationships where a redirect points back to its own source. I have also logged each detected circular redirect using Psr\Log\LoggerInterface and provides a user prompt to confirm the delete operation. Upon confirmation, it deletes all identified circular redirect records.

Drush command for the purging circular redirects: drush redirect:purge-circular-redirects

Please review this.

sourav_paul’s picture

Assigned: Unassigned » sourav_paul
sourav_paul’s picture

Assigned: sourav_paul » Unassigned
Status: Needs review » Needs work
StatusFileSize
new419.92 KB

MR was applied successfully but got a fatal error on running the command drush redirect:purge-circular-redirects
Using redirect 8.x-1.10 & D10.3
Sharing ss:
img

Please fix the logger issue...

uttam’s picture

Status: Needs work » Needs review

@sourav_paul Hello I have resolved the fatal error and tested the same in d10 and d11, the circular redirects are successfully purging. Please review this once.
Moving this to Needs Review

sourav_paul’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new236.62 KB

Thanks @uttam for your contribution. I've retested the MR, which was applied cleanly.
Drush command working as expected.

Attaching ss:
img

Hence Moving it to RTBC.

uttam’s picture

Hello @maintainers
Could you please test and review this feature request implemented on MR 103.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

Sorry for ignoring this for so long, this should be adjusted to more recent drush versions, drush no longer uses drush.services.yml I think.