Do you want to know how to Drupal?

Let's Drupal

How to create programmatically a redirect for Redirect module in Drupal 8?

If use Redirect module sometimes you need to create URL programmatically. You can do it using the code bellow

use Drupal\redirect\Entity\Redirect;

Redirect::create([
    'redirect_source' => 'your_custom_url', // Set your custom URL.
    'redirect_redirect' => 'internal:/node/[NID]', // Set internal path to a node for example.
    'language' => 'und', // Set the current language or undefined.
    'status_code' => '301', // Set HTTP code.
  ])->save();