MODX Community

Keep old resources URL when activate "use_alias_path" / or add automatically 301 redirect using SEO_Tab

HI :slight_smile:

I would like to activate “use_alias_path” after running the website for a while (don’t ask for the reasons :wink: ). Is it possible to keep the old urls of the existing resources?

There are a LOT of pages. So i don’t want to open all and activate “fix URL” or add the 301 redirect manually to “SEO Tab”.

Is there any solution for my Problem?

To add a new redirect to “SEO Tab” programmatically you can probably do something like that:

<?php
$modelPath = $modx->getOption('stercseo.core_path', null, $modx->getOption('core_path') . 'components/stercseo/') . 'model/';
$modx->addPackage('stercseo', $modelPath, null);

$new_redirect = $modx->newObject('seoUrl');
$new_redirect->set('url', urlencode('https://www.example.com/old_path'));
$new_redirect->set('resource', $resource_id);
$new_redirect->set('context_key', $context_key); //e.g. 'web'
$new_redirect->save();

Thanks for the mega fast reply!

I am just a designer with technical understanding, but without any programming skills :wink:

What I do not understand:

  1. Where do I use your code? I would have to automatically run through all resources, right?
  2. Where do I get the current URL of the resource that I have to enter as new redirect url?
  3. In your example the url is fixed in the code, isn’t it?
  1. You would put the code in a snippet and append it: Add a command to load all the resources $modx->getCollection('modResource') and then add a loop to add all the redirects. Then you would execute this snippet by putting it in a (temporary) page and and calling this page only once from the frontend.
  2. You can create the url from the resource-id with a call to $modx->makeUrl($resoure_id, '', '', 'full');
  3. Yes, the url here is fixed. It was just an example to show the general way of adding 1 new entry.

Thanks :slight_smile:
I will try it next week!