Switching Servers: Is it possible to rewrite dynamic URLs from old server?

Hello, I’m switching a blog from a ColdFusion server to an Apache server but keeping the same domain name. On the ColdFusion server, the blog was powered by BlogCFC and each post had a friendly URL (blogs.digitalprimates.net/dpnews/index.cfm/2008/2/8/Flex-Camp-Chicago-presentations) and a dynamic URL (blogs.digitalprimates.net/dpnews/index.cfm?mode=entry&entry=49A80088-A261-9EB1-429A25D0FEF53500), both of which were cached in Google search results. In the new blog, each post will have a URL in the following format: digitalprimates.net/news/2008/2/8/flex-camp-chicago-presentations

On the new web server I’m switching to, the blog will be using Wordpress. I’ve imported all of the old blog posts to Wordpress and was able to successfully rewrite the user friendly permalinks from the old using the following .htaccess rewrites:

RewriteRule ^dpnews/index.cfm/2008/2/8/Flex-Camp-Chicago-presentations$ http://digitalprimates.net/news/2008/2/8/flex-camp-chicago-presentations

I’m wondering if its possible to rewrite the old dynamic permalinks (blogs.digitalprimates.net/dpnews/index.cfm?mode=entry&entry=49A80088-A261-9EB1-429A25D0FEF53500) to their corresponding links in the wordpress blog. Can this be done through .htaccess? If not does anyone have any ideas on how to get around this problem. Many of these blog posts have a large number of permalinks to these dynamic URLs and I really want to try and keep them intact. Thanks in advance for any help!!

I9,

I’m here when I’m here and willing to help (members learn) when I can.

Was it not possible to dump your db from ColdFusion so you could insert all the records into WP’s db? That should have been easy - what db does CF use? How many records (articles) do you have stored? I recently took a massive Access db (which had been “converted” to MySQL format and it took hours to reformat correctly). Okay, a lot of that was due to the MB size of some of the tables and LACK of data dictionary but that’s a different question as this was and is your database. I’d look to make a direct move if possible or use some regex magic to make “universal” changes."

If that’s just not possible, I’d then fall back to converting the titles in the new DB to the same (Title capitalization - check mb_convert_case for how to do that. It would be easy to have a script rip through your DB reading, converting and writing titles back into the db in the title case format (which your example suggested). A little help from mod_speling could correct 1-2 capitalization changes, too, so don’t overlook that little gem!

Because a RewriteMap program is essentially just a look-up table using the old URI (in your case, query string) to fetch the new URI, why not build a script to do that and redirect those crazy query strings to that script - which would then send a 301 code followed by a header(location) redirection. It’s the same thing a RewriteMap would do for you and available via .htaccess.

Don’t forget to escape the dot character in the regex (unless within a character range definition)!

Aw, that would fail immediately as the RewriteRule can only examine the {REQUEST_URI} variable, not the {QUERY_STRING} entry value which you’re trying to have mod_rewrite map to a ‘title’ field (apples don’t mix with oranges very well).

Okay, I’ve given you several ways to beat this:

  1. Correctly transfer your db records - yes, entry can be transferred, too!

  2. “Massage” your ‘title’ string in the newly updated db to be title case.

  3. Use mod_speling to correct 1-2 typos and capitalization problems.

  4. Create your own pseudo RewriteMap handler in PHP and redirect there to have it redirect from your old title (OR old query string) to your new title. The only caution here is that you need to be sure that the old version of either title or entry exists before sending the 301 code and redirection ELSE sending to a 404 page (sitemap - which should be your new Home Page which had better have good links!).

To quote Tim Allen in Galaxy Quest, “Never give up! Never surrender!” There’s always more than one way to skin a cat - or code around a perceived problem.

Regards,

DK

Thanks for the quick reply DK! The reason I did a separate redirect rule per blog post was because, for some reason, when I imported the posts into wordpress, the titles didn’t always match. Basically, I have zero familiarity with ColdFusion and there is not clean-cut tool to import posts form BlogCFC to Wordpress (that I could find), so I used some online tutorials/accounts of semi-successes haha and stumbled through it. But since I couldn’t get the titles to match up, I couldn’t do one redirect rule.

For the query string URLs, basically I just want all the permalinks that are floating out on the web to them to remain functional (ie not spit a 404 error). I am with you 100% haha that the IDs used by BlogCFC are ridiculous. But when I switch the DNS, I’d like to have the query string URLs for the old blog to be interpreted correctly and linked to the corresponding blog post. Functionality wise, I’d like this rule:

RewriteRule ^dpnews/index.cfm/2008/2/8/Flex-Camp-Chicago-presentations$ http://digitalprimates.net/news/2008/2/8/flex-camp-chicago-presentations

to work with the query string URL:

RewriteRule ^dpnews/index.cfm?mode=entry&entry=49A80088-A261-9EB1-429A25D0FEF53500$ http://digitalprimates.net/news/2008/2/8/flex-camp-chicago-presentations

however I know this doesn’t work. I know wordpress doesn’t use this crazy format. Wordpress uses the exponentially simpler domain.com/?p=123 format. And then through Wordpress, these URLs are rewritten to user-friendly ones.

So if I’m understanding you correctly, I’d need to use a RewriteMap to pull the variables from the old ColdFusion URLs and then correctly map them to the correct post (year, month, day, title) in Wordpress? So there is no way to do this through the .htaccess file? Thanks again for helping me out!

I9,

First, WELCOME to SitePoint’s Apache forum!

As for your RewriteRules, you’ve MISSED THE BOAT! If you’re going to add one statement per redirection, use mod_alias’s Redirect as that does not require Apache’s regex engine. Even that misses the boat, though, as, if you bring the POWER of Apache’s mod_rewrite to bear, ONE STATEMENT CAN DO IT ALL! For example:

RewriteEngine on
RewriteRule ^dpnews/index\\.cfm/([\\d]{4})/([\\d){1,2})/([\\d]{1,2])/([-a-zA-Z]+)$ news/$1/$2/$3/$4 [R=301,L]

That code looks for dpnews/index.cfm then captures the year, month, day and ‘title’ from the remainder of the URI and redirects to news followed by the year, month, day and ‘title.’ THAT, sir, is the POWER regex brings to mod_rewrite!

As for your query string format, I doubt that WP would use the same mode=entry and entry={ridiculous alphanumeric string} therefore your question begs the return question: What do you want to do with it?

Lacking that information now, I can say that, IF (and ONLY IF) you have access to the server or virtual host configuration file, you can use a RewriteMap to map from {ridiculous alphanumeric string} to the same year, month, day and title string (or WP article id, if that’s what floats your boat).

If that’s whet your appetite to LEARN about mod_rewrite, have a read of the tutorial Article linked in my signature.

Regards,

DK