x
 
Create New Page
 View Previous Changes    ( Last updated by Lisa Wess )

Redirecting pMachine URLs

Question:

After converting my weblog from pMachine to ExpressionEngine, how can I redirect the old URLs to the new locations?

Answer:

Overview

ExpressionEngine has a built-in facility for redirecting URLs to pMachine weblog articles to their new EE URLs.
However, this does still need some outside help.

‘’‘The following discussion assumes that you are running with an Apache webserver with mod_rewrite.
If you aren’t, this probably won’t help you too much.’‘’

Create a template for redirected URLs

EE will redirect all pMachine URLs to one specific template.
If you only had one weblog, then you can simply use the “weblog/index” template for your new weblog.
But if you had multiple weblogs before, you probably will need to create a special template for the purpose.
This template will be very similar to weblog/index, except that in the {exp:weblog:entries} tag, the “weblog=” parameter will list all of the weblogs that might need to be accessed for redirected URLs.

To prevent search engines from seeing a duplicated article
(one at weblog/index/some_title and one at weblog/legacy/some_title),
you can add the following line to the <head> section of the page:
<div class=“codeblock”> <meta name="robots" content="noindex,nofollow"> </div>

Turn on EE’s pMachine redirection

In the Control Panel, under the Admin tab, select “Global Weblog Preferences”.
Toward the bottom of the preferences you’ll find a radio button to turn on “Remap pMachine Pro URLs to EE”. You’ll also find a place to specify the URL for your redirected URLs (see above).

Edit your .htaccess file

Okay, this can get tricky.
If you get something wrong here, you can make it so nobody can reach your site.
Be sure to test that your site is still working after each change.

Unless you know otherwise, your .htaccess file is located in the same directory that your index.php file (or whatever you renamed it to) is located in.
You can edit it with whatever tool you’re comfortable with.
You probably want to save a backup copy before you start editing!

Here are the lines that you will be adding.
Check each line for things that you need to change for your particular installation;
this example assumes that you’re using the installation defaults so that your main web page is located at /index.php/weblog/index/, and that your old pMachine installation used the default files of weblog.php, more.php, comments.php, archives.php, and archivesum.php.

Header

<IfModule mod_rewrite.c>
RewriteEngine On

You may already have lines like these. You only need them once; don’t add in a second copy.

Redirect weblog.php

RewriteCond %{QUERY_STRING} ^id=D([0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9]) [url=NC]NC[/url]
RewriteRule
^weblog\.php /index.php/weblog/%1/%2/%3/? [R=301,L]

This rule redirects “daily” pages.

RewriteCond %{QUERY_STRING} ^id=C[^_]*_1_.* [url=NC]NC[/url]
RewriteRule
^weblog\.php /index.php/weblog/category/general/? [R=301,L]

This rule redirects “category” pages. You need to repeat this rule for each category!
Replace the “1” in the RewriteCond line with a pMachine category number,
and the “general” in the RewriteRule line with the EE category name.

RewriteCond %{QUERY_STRING} ^id=[url=ADP]ADP[/url]?([0-9]+) [url=NC]NC[/url]
RewriteRule
^weblog\.php /index.php?id=%1_0_1_0 [R=301,L]
RewriteRule
^weblog\.php /index.php [R=301,L]

The first rule redirects the requests for individual articles. The last rule is a “catch-all” for anything that got missed, including requests for the weblog’s “home page”.
The query strings are cleaned up so that the EE remapper can work.

Redirect more.php

RewriteCond %{QUERY_STRING} ^id=[url=ADP]ADP[/url]?([0-9]+) [url=NC]NC[/url]
RewriteRule
^more\.php /index.php?id=%1_0_1_0 [R=301,L]
RewriteRule
^more/[url=ADP]ADP[/url]?([0-9]+) /index.php?id=$1_0_1_0 [R=301,L]
RewriteRule
^more\.php/[url=ADP]ADP[/url]?([0-9]+) /index.php?id=$1_0_1_0 [R=301,L]
RewriteRule
^more\.php /index.php [R=301,L]

These rules redirect queries for pMachine “more” pages. The two middle rules are for search-engine-friendly queries.
The last rule is a “catch-all” for anything that got missed.
The query strings are cleaned up so that the EE remapper can work.

Redirect comments.php

RewriteCond %{QUERY_STRING} ^id=[url=ADP]ADP[/url]?([0-9]+) [url=NC]NC[/url]
RewriteRule
^comments\.php /index.php?id=%1_0_1_0 [R=301,L]
RewriteRule
^comments/[url=ADP]ADP[/url]?([0-9]+) /index.php?id=$1_0_1_0 [R=301,L]
RewriteRule
^comments\.php/[url=ADP]ADP[/url]?([0-9]+) /index.php?id=$1_0_1_0 [R=301,L]
RewriteRule
^comments\.php /index.php [R=301,L]

These rules redirect queries for pMachine “comments” pages. The two middle rules are for search-engine-friendly queries.
The last rule is a “catch-all” for anything that got missed.
The query strings are cleaned up so that the EE remapper can work.

Redirect archives.php

RewriteCond %{QUERY_STRING} ^id=A?(....)(..) [url=NC]NC[/url]
RewriteRule
^archives\.php /index.php/weblog/%1/%2/? [R=301,L]
RewriteRule
^archives/A?(....)(..) /index.php/weblog/$1/$2/ [R=301,L]
RewriteRule
^archives\.php/A?(....)(..) /index.php/weblog/$1/$2/ [R=301,L]

These rules redirect queries for pMachine “archives” pages. The last two rules are for search-engine-friendly queries.

Redirect archivesum.php

RewriteCond %{QUERY_STRING} ^view=date [url=NC]NC[/url]
RewriteRule
^archivesum\.php /index.php/weblog/archives/? [R=301,L]

This rule redirects queries for archive index by date. This rule assumes that you have an ‘archives’ template. One place to get one is from the Notebook template.

RewriteCond %{QUERY_STRING} ^view=category [url=NC]NC[/url]
RewriteRule
^archivesum\.php /index.php/weblog/categorized/? [R=301,L]

This rule redirects queries for archive index by category. This rule assumes that you have a ‘categorized’ template that performs a Category Archive listing.

Trailer

</IfModule>

Again, you may already have a line like this. You only need it once; don’t add in a second one.

Question:

How can I redirect URLs if the IDs for my imported entries don’t match up with the old pMachine IDs? When people click on an old link, they are sent to the wrong page!

Answer:

Using your .htaccess file, a little bit of PHP and your old pMachine database, you can easily point a user’s browser to the right entry. In order to import your old pMachine entries, you had to create a database that EE can import from. Hopefully, you haven’t deleted that database. This will not work without it.

Edit your .htaccess file

Be careful here. You don’t want to cripple your site by messing up something in this section.

You’re going to add a line to your .htaccess file that will take the old pMachine formatted URLs and convert them for use with a PHP file. Our example below takes into account the different comment pages we had in our old pMachine site:

RedirectMatch permanent ^/(business|culture|politics|travel|hispanics)_comments/[A]?([0-9]+) /redirect.php?id=$2\&area=$1/

Basically, this will look for URLs that contain business_comments, culture_comments, politics_comments, etc. It will strip the first part out and use it to know which template group to send the user to. The next chunk it strips out is the old ID, whether it contains an A in front or not. This is then passed to the PHP file shown below.

The redirect.php file

<?php
$id     
= ( isset( $_GET['id'] ) ) ? $_GET['id']: '';
$area    = ( isset( $_GET['area'] ) ) ? $_GET['area']: '';

// If no area is passed, send them to the homepage

if ( $area == '' )
{
header
( "location:/" );
}

// If no ID is passed, send them to the right template group at least

if ( $id == '' )
{
header
( "location:/$area" );
}

//-------------------------------------------------
// Create URL Title
//-------------------------------------------------

function create_url_title($str)
{
$str
= strip_tags(strtolower($str));
$str = preg_replace('/\&#\d+\;/', "", $str);

$trans = array(
"-" => '_',
"\&\#\d+?\;" => '',
"\&\S+?\;" => '',
"['\"\?\.\!*\$\#@%;:,\-=\(\)\[\]]" => '',
"\s+" => '_',
"\/" => '_',
"[^a-z0-9-_]" => '',
"_+" => '_',
"\&" => '',
"_$" => '',
"^_" => '' );

foreach (
$trans as $key => $val)
{
$str
= preg_replace("#".$key."#", $val, $str);
}

$str
= trim(stripslashes($str));

return
$str;
}

// END


$hostname    = "localhost";
$username    = "YOUR_USERNAME";
$password    = "YOUR_PASSWORD";
$db_name    = "YOUR_DB_NAME";

$conn_type    = 0; // 1 = persistent 0 = non-persistent

$link     = mysql_connect($hostname, $username, $password)
or die(
'Not connected : ' . mysql_error());

$db     = mysql_select_db($db_name, $link)
or die(
"Couldn't select database");

// Execute the query

$sql    = "SELECT title FROM pm_weblog WHERE post_id = '".$id."' LIMIT 1";

$query    = @mysql_query($sql,$link)    or die("Couldn't execute delete query.");

// If we don't get any results, send them to the right template group

if ( mysql_num_rows($query) != 1 )
{
header
("location:/$area");
}

// Send them to the right template group with the newly made url_title that EE can understand

while ( $row = mysql_fetch_array($query) )
{
$title    
= create_url_title( $row['title'] );

header("location:/$area"."entry/$title");
}

mysql_close
( $link );

?>

This file simply checks for the $id and $area variables. When an $id is passed to it, it checks the old pMachine database for the entry’s title. It then converts that title into an EE-compatible url_title and redirects their browser. The new URL is formatted to work with your EE template groups. You will probably need to change the format of the redirect to work with your site.

Important reminder

The .htaccess file is very powerful.
You can do some amazing things with it.
You can also screw things up pretty badly with it.

So be sure that you save a backup copy before you ever mess with the file.
And be sure to test your website after each change, to be sure that it’s still working.

Category:Import

Categories: