0

I am currently working on a big project for a client where we are importing all of their content into ExpressionEngine. I written a script to do this which is fine, however, all of the URLS are going to be different for the new pages.

I have created an extra field in the channel for these pages which contains the pages old URL, could someone suggest a way to, when the old url is hit, to find that in the channel entries and redirect (301) to the new url?

Cheers.

0

Since you have added a field containing the old URL you could do something like this.

  1. Ensure all "old" URLs land on your 404 template

  2. Set up the 404 template with an entries tag pair which uses the search parameter to find the entry. search:"oldUrlField=xxx" (need something to grab the full URL, maybe an add-on such as Mo Variables, maybe some PHP). This will find the entry you really need to show.

  3. Use the redirect global variable to send the visitor to the new URL, issuing a 301 http code.

3
  • 1
    I like your solution too. It's clever! But I believe Query Module will provide a better performance, specially because the 404 page will be accessed a lot of times!
    – Sobral
    Sep 5 '14 at 12:42
  • This worked perfectly to what I needed to do. Cheers!
    – Scott
    Sep 5 '14 at 14:20
  • Yes, I imagine creating a custom query would possibly be better. You could certainly reduce the amount of stuff you're pulling out of the database.
    – foamcow
    Sep 5 '14 at 16:17
0

If there's some part which can just be removed to the new URL, you can do this easily using redirect on .htaccess or NGINX locations:

rewrite "^/blog/\d{4}/\d{2}/\d{2}(.*)$" http://domain.com/blog$1 permanent;

That is an example I used on NGINX to redirect old WordPress style URLs to new ones.

If this isn't an option and you don't have too many redirects to do, you can create a template using exp:channel:entries to write the redirect rules.

NGINX:

{exp:channel:entries channel="channel_name" limit="500"}
rewrite ^/{old_url} http://domain.com/template_group/{url_title} permanent;<br />
{/exp:channel:entries}

Apache:

{exp:channel:entries channel="articles" limit="500"}
RewriteRule ^/{old_url} {site_url}/blog/{url_title} [L,R=302]<br />
{/exp:channel:entries}

Access the template, copy and paste the rules on your .htaccess file or NGINX location.

IMPORTANT

Remember to test using temporary redirects first!

0

I'm not sure it will help you 100% in this instance and I have not used this add-on but you might want to look at Detour Pro to help manage the necessary htaccess redirects directly from within your EE Control Panel.

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.