Jump to content

Redirect old urls using head.inc and API


woop
 Share

Recommended Posts

Hi! I'm no php expert but I thought I should share my way of redirecting URLs from my old site setup. I'm going to use Drupal urls in this example, but should work for any url. I can't guarantee this is the best way of doing it, so please feel free to correct me if you have feedback regarding security, speed or similar.

Step 1:

Make sure that each page in PW has some kind of connection to its previous URL so it can recognize when a redirect needs to be done. I'm going to use the node id:s and taxonomy id:s that my pages had when they were located in Drupal. I've made sure to save them all into each processwire page in a field called "drupal_id". Maybe you could just save the old URL in a field when migrating and use that, as an alternative.

Step 2:

Open head.inc and add this to the very top:

$thisurl = $sanitizer->url("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");

// Check if the URL reminds you of the old url scheme
if (preg_match("/com\/\?q=/", $thisurl)){
	// if it looks like a node url
	if (preg_match("/\?q=node\/[0-9]*/", $thisurl, $nodeslug)){
		$oldnid = preg_replace("/\?q=node\//", "", $nodeslug[0]);
		if ($match = $pages->get("drupal_id={$oldnid}")){
			$session->redirect($match->httpUrl);
		} else {
			$session->redirect($config->urls->root); 
		}
	// If it's a taxonomy url
	} elseif (preg_match("/\?q=taxonomy\/term\/[0-9]*/", $thisurl, $taxonomyslug)) {
		$oldtid = preg_replace("/\?q=taxonomy\/term\//", "", $taxonomyslug[0]);
		if ($match = $pages->get("drupal_id={$oldtid}")){
			$session->redirect($match->httpUrl);
		} else {
			$session->redirect($config->urls->root);
		}
	} else {
		$session->redirect($config->urls->root); 
	}
	
}

Regex is used to find URL schemes and extract drupal nids and tids from the url. These are then looked up using the api, only to return the new url. Each time the url redirect fails, it might be suitable presenting a warning to the user. I've added something like $session->message("This URL seems to have changed. Please use the search if you didn't find what you were looking for") on the line before redirect (not part of the code example above, for simplicity's sake).

Hope this can be useful for someone else, too. And again, feel free to leave feedback! I'm here to learn, too :)

  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By pwuser1
      First I am a PW student I think it's the best PHP Framework.  So please don't get me wrong when I asked when other Frameworks like the big 3 (Wordpress, Joomla, Drupal) are too complicated to use or maintain (I tried them all) why are people still using them? PW is just too elegant in comparison even for a newbie like me.
    • By anthonygustin
      Hey all, things had been going well remapping a site from one domain to another, but hit a snag and have searched for hours and don't know how to continue. 
      1) I was moving everything from thepaleofix.com to dranthonygustin.com
      2) I moved every single path individually with redirects like this: Redirect 301 /category/post http://www.dranthonygustin.com/category/post
      Everything smooth sailing until today when through webmaster tools (to say site address as moved) I had to execute "Redirect 301 / http://www.dranthonygustin.com" -- now some of the urls go through and some don't include the / after the .com extension. 
      For example:
      http://www.thepaleofix.com > http://www.dranthonygustin.com no problem 
      http://www.thepaleofix.com/resource/purepharma-m3-review/ > http://www.dranthonygustin.com/what-does-magnesium-do/ no problem
      BUT a bunch of the URLs then don't push... For example:
      http://www.thepaleofix.com/recipe/paleo-jam > http://www.dranthonygustin.comrecipe/paleo-jam/ (notice NO / after the .com extension)
      and http://www.thepaleofix.com/wp-admin > http://www.dranthonygustinwp-admin (which of course leads to dead link)

      There are a bunch of paths that do this, that I checked before and were working, such as: 
      Redirect 301 /recipe/paleo-jam http://www.dranthonygustin.com/simple-vanilla-berry-paleo-jam/
       
      Any idea why after implementing the / redirect, why it broke some of the redirects and not all? 
       
      Thanks! 
    • By Nate C.
      Hello All, I was hoping someone could help me with a Redirect issue. I have a new site created that is replacing an old version. We got rid of a bunch of subpages and are trying to get the old URLs to relink to the corresponding new pages. This works on some of them, bringing them back to he main page and a certain section (its a one page scrolling site for the most part) but the ones that are supposed to go to the sub-pages are not working.
      I have tired a bunch of different permutations with no luck. The old site used Wordpress and the current one does not, if that is a factor. This is my first real site-build, so any help would be greatly appreciated - thanks!
      Here is the code I am trying to use, the redirects work properly up through the 'Careers' section:
      Options +FollowSymLinks RewriteEngine on #301 Redirect Old File Redirect 301 /contact http://www.mngdirect.com/#contact Redirect 301 /about http://www.mngdirect.com Redirect 301 /careers http://www.mngdirect.com/#careers Redirect 301 /our-team http://www.mngdirect.com/#teams Redirect 301 /about/careers http://www.mngdirect.com/#careers Redirect 301 /solutions/eventdirect http://www.mngdirect.com/eventdirect Redirect 301 /solutions/marketingdirect http://www.mngdirect.com/marketingdirect Redirect 301 /solutions/eventdirect/eventdirect-peer-to-peer/ http://www.mngdirect.com/eventdirect Redirect 301 /solutions/eventdirect/eventdirect-advisory-board/ http://www.mngdirect.com/eventdirect Redirect 301 /solutions/eventdirect/eventdirect-broadcast/ http://www.mngdirect.com/eventdirect Redirect 301 /solutions/eventdirect/eventdirect-speaker-training/ http://www.mngdirect.com/eventdirect Redirect 301 /solutions/marketingdirect/marketingdirect-triggered-formulary-campaigns/ http://www.mngdirect.com/marketingdirect Redirect 301 /solutions/marketingdirect/marketingdirect-triggered-attendee-campaigns/ http://www.mngdirect.com/marketingdirect Redirect 301 /solutions/marketingdirect/marketingdirect-scheduled-speaker-campaigns/ http://www.mngdirect.com/marketingdirect Redirect 301 /solutions/marketingdirect/marketingdirect-scheduled-target-campaigns/ http://www.mngdirect.com/marketingdirect Redirect 301 /solutions/marketingdirect/marketingdirect-triggered-rep-campaigns/ http://www.mngdirect.com/marketingdirect Redirect 301 /solutions/marketingdirect/marketingdirect-triggered-behavior-campaigns/ http://www.mngdirect.com/marketingdirect #RedirectMatch 301 ^/solutions/ http://www.mngdirect.com/#solutions #error 404 ErrorDocument 404 /404.html -Nate
    • By mediamar
      I moved an old site to processwire and want to redirect the old URLs with php-filenames to the new folders via .htaccess to match the new structure.
      I tried:
      RedirectMatch 301 /filename.php http://example.com/filename/
      The Browser end up like this:
      http://example.com/filename/?it=filename.php
      Its interfering with that Processwire .htaccess rule
      # ----------------------------------------------------------------------------------------------- 
        # Pass control to ProcessWire if all the above directives allow us to this point.   # For regular VirtualHosts (most installs)   # -----------------------------------------------------------------------------------------------     RewriteRule ^(.*)$ index.php?it=$1 [L,QSA]   If i comment that out the redirect in the URL is ok, but every Klick on a websitelink is showing the homepage.   Any idea how i can fix this? Thanks   Andreas
    • By kreativmonkey
      Hi,
      i want to import stuff from Drupal to Processwire and have create an importscript but some things doesn't work.
      I import a list of questions from users and i will manipulate the page created date (thats work fine) and change the User to the right one. If the User dosn't exist, i will creat a new one. But this is the first problem i have, no user will be change or create...
      The second problem is to find out if the page does alrady exist or not. If the Page exist i will only modify some fields, otherwise i will create a new page.....
      To find out if the Page exist and whats the Page ID do i also need to import the Comments.....
      readCSV.php:
      // Read the CSV file and get an array of rows function readCSV($file){ $lines = array(); foreach(file($file, FILE_IGNORE_NEW_LINES) as $line){ $lines[] = str_getcsv($line); } return $lines; } import.php:
      while($i < $countArray){ // Initzalisierung der Variablen $title = $data[$i][0]; $date = $data[$i][6]; #$summary = ''; $body = $data[$i][1]; $categorys = $data[$i][4]; $tags = $data[$i][3]; #$images = ''; $authorName = $data[$i][5]; $sanitizTitle = $sanitizer->pageName($title); $post = $pages->get("template=$template, name=$sanitizTitle"); if(empty($post)){ $p = new Page(); // create new page object $p->template= $template; //set template post $p->parent = wire('pages')->get('/$parent/'); //set the parent $p->name = $sanitizTitle; // SANITIZE!!!! $p->title = $title; // added by Ryan: save page in preparation for adding files (#1) $p->save(); $p->created = $date; // Timestamp!!! $p->save(array('quiet' => true)); // To save the created Date $p->body = $body; // $p->summary = $summary; // Looking for the inizialisation $p->tags = $tags; $p->categories = $categorys; $p->save(); echo 'Created Page: '. $title .'<br>'; $authorNameSanitiz = $sanitizer->name($authorName); if($users->get($authorNameSanitiz)){ $authorID = $users->get($authorNameSanitiz); $p->createdUser->name = $authorID; } else { $u = new User(); $u->name = $authorNameSanitiz; $u->addRole("author"); $u->addRole("guest"); $u->save(); echo 'creat User: '. $authorName .'<br>'; $authorID = $users->get($authorNameSanitiz); $p->createdUser = $authorID; } $p->save(); #} // populate fields //$p->images->add('path/to/image1.jpg'); // add multiple to images field $i++; } The CSV:
      "Titel","Inhalt","E-Mail","Schlagworte","Thema","Benutzername","Beitragsdatum" "digitale Filme in der Strahlentherapie","Hi,<br>zur Zeit suchen wir in unserer Abteilung für Strahlentherapie in Regensburg eine digitales Filmsystem.<br>Welche Firmen bieten den solche Systeme für die Strahlentherapie an?<br>MfG<br>Jan<br>","janmee@web.de","","Strahlentherapie","pete","1437587189" "Gehalt MTA R","Hallo,<br>ich interessiere mich für den Beruf. Wo kann ich erfahren, was ich ausgelernt verdiene? Gibt es einen Tarifvertrag? In welchem ist man da wie eingruppiert? Vielen Dank<br> <br> <br> <br> <br> <br>","sweetmic@web.de","Gehalt","Radiologie","guest","1437303471" what am i doing wrong?
×
×
  • Create New...