1

So I've recently moved a blog from Wordpress to ExpressionEngine. Everything transferred over great (thanks to DataGrab!!).

Now the one problem I'm having is in my attempt to redirect traffic from the old blog to the new one. I've implemented an HTACCESS like so:

RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.new_ee_site.com/blog/view/$4 [R=301,NC,L]

To redirect the old blog posts to their new counterparts on the EE install.

Here's the problem: This works for post with short titles just fine, the problem is some post titles are greater than 75 characters (the default EE url_title limit). So the post with titles name greater than 75 aren't translating over (since, when importing, the blog titles for the EE blog got trimmed to 75 characters).

Is there a way to somehow truncate incoming URLs, if they match the format /blog/view/..blog-title.., to truncate / trim the blog-title portion to 75 characters so these links go to the right place?

2

Using the info from the link provided by AllinOne https://stackoverflow.com/questions/1649435/regular-expression-to-limit-number-of-characters-to-10 you should be able to solve this. The information is relevant to your problem as it lets you match a number of characters in a given string.

Try

RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(.*){1,75}$ http://www.new_ee_site.com/blog/view/$4 [R=301,NC,L]

I'm not sure if you will need to change the (.*) to something like ([a-z][A-Z][0-9]){1,75} but effectively you are saying grab the first 75 characters of the last segment of the url.

I've had a quick play around and this might be better. It depends on what characters are in your WP URLs. This one should match upper and lower case characters, hyphens, and underscores. It also assumes that the first 3 segments of your URLs are digits (are they dates?).

^(\d{1,4}/){3}([a-z]|[A-Z]|[0-9]|-|_){1,75}

EDIT

Try this regex instead

^([0-9]+\/){3}([a-zA-Z0-9\-\_]{0,75})
| improve this answer | |
  • I tried a simplified version of your regex just to see if I could get the redirect to work, and it doesn't work at all. I tried RewriteRule ^([0-9]+\/){3}/([a-zA-Z0-9\-\_]+)$ http://www. new_ee_site.com/blog/view/ [R=301,NC,L] just to get a generic redirect working with it before I started to fine-tune, and nothing? Thanks for the suggestion though... – shparkison Aug 8 '14 at 15:04
1

Thinking on my feet here.

I don't think you can do this via htaccess so what I would do is ensure that these urls end up at a special template (you might do it on a 404 page but I'm not sure of the SEO implications there).

Then on that special template I would look for the presence of 'blog' in segment_1 then grab segment_3, truncate it (with PHP or a plugin) and then feed the result into either a redirect or directly into a channel:entries tag.

You should also respond with a 301 http code so the referring URL can make any changes needed (Google etc will like this!)

I've used this technique with 404 pages before when moving a site from one platform to another or after a big change in url structure and it's been quite successful.

This feels really clumsy and there may well be a more elegant way to do it. Like I said, thinking on my feet over morning coffee!

| improve this answer | |
  • Yes. I like this approach and use it myself. We do it a little differently tho. The 404 page looks to see if it's the sort of URL we want to preserve ('blog' in segment_1, in your example) if it is we run through a translation table of all the urls we want to prevent from breaking. This gives us the ability to preserve urls that don't follow a specific rule... maybe the url was changed after import to the new site for example. Then we return either 404 or 301 depending if we got a match. – AllInOne Aug 8 '14 at 16:32
1

Ok, thanks to both @foamcow and @AllInOne I was able to weed out the solution.

So here is what works:

RewriteBase /
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/([a-zA-Z0-9\-/_]{0,75})(.*)$ http://www. new_ee_site.com/blog/view/$4 [R=301,NC,L]

Basically, I used

([a-zA-Z0-9-/_]{0,75})

to match the 75 characters I was after, then I had to add the

(.*)

Because otherwise it didn't match any URLs that had a title greater than 75, so i had to take that into account. Now the $4 variable contains the first 75 characters of the URL, and the other variables aren't used, but 1-3 are the date, and 5 is the part that gets trimmed off.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.