Creating URL Redirects with Migrate Module
New versions of websites often have new content organizations with new URL patterns. When old content is assigned a new URL it's almost always desirable to redirect traffic from the content's old URL to its new URL. The redirect preserves external links to the site content (i.e., prevents "link rot") and helps maintain search engine page ranking.
If the Drupal Migrate module is used to create the content for the new site there's an easy code addition for creating the redirect at the same time as the content is created. This method uses the code described in the post: Programatically Create 301 Redirects.
The Migrate framework calls the complete method when an entity, for example a node, is successfully created. Information on the complete method can be found in this documentation: Commonly implemented Migration methods.
The complete method is passed the entity created and the row data used to create it. This provides a place to create a redirect using the Redirect module.
In the following example, the old path is part of the row data. Depending on your situation this information could alternatively come from a pre-generated lookup or separate function call.
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">class </span><span style="color: #0000BB">ExampleMigration </span><span style="color: #007700">extends </span><span style="color: #0000BB">Migration </span><span style="color: #007700">{<br> <br> </span><span style="color: #FF8000">//<br> // Migratation code<br> //<br><br> /**<br> * Definition for Migration complete() method<br> */<br> </span><span style="color: #007700">function </span><span style="color: #0000BB">complete</span><span style="color: #007700">(</span><span style="color: #0000BB">$entity</span><span style="color: #007700">, </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Create a redirect from the old path to the new one<br> </span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">old_path</span><span style="color: #007700">)) {<br> </span><span style="color: #FF8000">// Create an object with our redirect parameters<br> </span><span style="color: #0000BB">$redirect </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdClass</span><span style="color: #007700">();<br> </span><span style="color: #0000BB">$redirect</span><span style="color: #007700">-></span><span style="color: #0000BB">source </span><span style="color: #007700">= </span><span style="color: #0000BB">$row</span><span style="color: #007700">-></span><span style="color: #0000BB">old_path</span><span style="color: #007700">; </span><span style="color: #FF8000">// From URL<br> </span><span style="color: #0000BB">$redirect</span><span style="color: #007700">-></span><span style="color: #0000BB">source_options </span><span style="color: #007700">= array();<br> </span><span style="color: #0000BB">$redirect</span><span style="color: #007700">-></span><span style="color: #0000BB">redirect </span><span style="color: #007700">= </span><span style="color: #DD0000">'node/'</span><span style="color: #007700">. </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-></span><span style="color: #0000BB">nid</span><span style="color: #007700">; </span><span style="color: #FF8000">// To URL<br> </span><span style="color: #0000BB">$redirect</span><span style="color: #007700">-></span><span style="color: #0000BB">redirect_options </span><span style="color: #007700">= array();<br> </span><span style="color: #0000BB">$redirect</span><span style="color: #007700">-></span><span style="color: #0000BB">status_code </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #FF8000">// Redirect Status<br> </span><span style="color: #0000BB">$redirect</span><span style="color: #007700">-></span><span style="color: #0000BB">type </span><span style="color: #007700">= </span><span style="color: #DD0000">'redirect'</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">$redirect</span><span style="color: #007700">-></span><span style="color: #0000BB">language </span><span style="color: #007700">= </span><span style="color: #0000BB">LANGUAGE_NONE</span><span style="color: #007700">;<br> <br> </span><span style="color: #FF8000">// Create the redirect<br> </span><span style="color: #0000BB">redirect_save</span><span style="color: #007700">(</span><span style="color: #0000BB">$redirect</span><span style="color: #007700">);<br> }<br> }<br> <br>}<br></span><span style="color: #0000BB">?></span></span>
Tagged: