Forums

Mass Redirects - How? (4 posts)

  1. ckeck
    Member
    Posted 1 year ago #

    Really hoping someone can help me out...not sure the best method for this situation. I'm helping someone migrate a large blog to a new domain and everything went smooth except the blog owner wants to leave a landing page at the old domain and individually redirect each former blog post to each post on the new URL.

    Is there an easy/simple way to accomplish this or does this mean I'd have to write individual 301 redirect rules for EACH post...over 100 of them...tell me I'm missing an obvious solution please :)

  2. Curtiss Grymala
    Member
    Posted 1 year ago #

    What I normally do is create a PHP file with an array of the redirects. I use the old location as the key and the new location as the value for each array element. I then check to see if the current location is within the array keys, and redirect to the array value if it is.

    The code might look something like:

    <?php
    $redirects = array(
    	'what.html' => 'what/',
    	'extended.html' => 'what/extended/',
    	'who.html' => 'who/',
    	'staff.html' => 'who/staff/',
    	'staffbios.html' => 'who/staff/#staff-bios',
    	'fundraising.html' => 'partnering/',
    	'facility.html' => 'about-us/#our-facility',
    	'faq.html' => 'about-us/faq/',
    	'news.php' => 'news/',
    	'calendar' => 'news/#calendar',
    	'newsarchive.html' => 'news/',
    	'contact.html' => 'contact/',
    	'jobs.html' => 'about-us/jobs/',
    );
    
    $filename = basename( $_SERVER['REQUEST_URI'] );
    
    if( array_key_exists( $filename, $redirects ) ) {
    	header( "Location: " . $redirects[ $filename ] );
    }
    ?>

    I then require that file (which I usually call redirects.php) at the top of my index.php or header.php file.

    You can probably find a way to dump the old locations into a PHP array (or at least a list that you can turn into an array) fairly easily. Good luck with it.

  3. ckeck
    Member
    Posted 1 year ago #

    Interesting, thanks for sharing. I'm not that familiar with writing PHP code directly so I was hoping for a plugin that could simple swap the root domain as the rest of the URL structure is exactly the same.

    Nothing out there like this?

  4. Curtiss Grymala
    Member
    Posted 1 year ago #

    I didn't realize the rest of the structure was the same, and that only the domain was changing. Can't you just forward or park the old domain on the new domain?

Topic Closed

This topic has been closed to new replies.

About this Topic