Drowning in a Sea of Dead Links

So there was a problem.  Shocking, I know.

The new site development is humming along (mostly)- and might be ready for launch soon.  The problem is the old url links.  The old site was not created using any kind of framework, it was just simple static html pages.  The new site has multiple frameworks that need to play nice together.  That’s not a problem, except with search engine optimization.  Apparently (and I’m no SEO expert), changing all your url routing can negatively affect your search results.  Links will be found that go to old, dead pages- or worse, nowhere.  Frameworks like Django and Angular don’t use the ‘.html’ suffix generally, so this could mean having to create a bunch of server url redirects.

Which gave me an idea.  Is it a good one?  Not sure, but it did seem easier and more efficient than a whole bunch of server redirects.

It goes like this: What is the Django urls.py file but a kind of layer on top of your web server?  It takes an incoming url string, matches it against a regex, and passes it along to the view.  So, why can’t we use that to redirect our old urls to the new method?

The answer is: we can!  One simple Django url setup:

url(r'^(P<path>[a-zA-Z0-9_-]+).html$', old_url_redirects)

And a slightly more complicated view setup (condensed for brevity’s sake):

def old_url_redirects(request, path):
    if path in {'new-route-one', 'new-route-two'}:
        category = 'new-middleman-here'
    return HttpResponseRedirect('/newfolder/'+category+'/'+path)

And there you have it!  The view function is a bit ugly because we reorganized the structure a bit to add categories into the url string (the old method just had one massive folder with all the endpoints).  If you don’t need that, you don’t need the ‘if path in’ conditional, and can remove the category variable from the return statement.  But the cool part is the ability to make a simple check over multiple options with a Python set.  So if someone gets the old link (http://www.mainsite.com/url-here.html), it will automatically redirect to the new route (http://www.mainsite.com/correct-category/url-here).

Does this method suck?  Maybe- but it does work!  If I’m missing something, or you see a more efficient way, help me out in the comments…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s