Awkward URL rewrite question
January 18, 2017 11:30 AM   Subscribe

Can anyone help me set up a redirect so that people using old links to my site can get to my new ones?

Long story short: I'm moving from Movable Type to WordPress, all my blog entries are imported but in a parent directory, and in a slightly different format.

Basically, I want anyone using one of my previous links, in the following format:

http://macdaraconroy.com/macro/year/date/article_name_whatever.html

to redirect to:

http://macdaraconroy.com/year/date/article_name_whatever/

In other words, moving up a directory, and lopping off the .html (the end slash doesn't matter, WordPress seems to already have a mod_rewrite rule for that internally).

So, how do I go about doing this? Reading mod_rewrite documentation is giving me a headache, and the examples I can find are either slightly too simple for what I need, or far too complicated that my eyes glaze over. But logically, it should be a simple thing to do, right? Which is why I thought someone here with actual experience in this kind of thing would have a solution. Please?
posted by macdara to Computers & Internet (5 answers total) 1 user marked this as a favorite
 
Best answer: You want something like this:

RewriteRule ^macro/(.+)/(.+)/(.+).html$ /$1/$2/$3/ [R,L]
posted by kyten at 12:04 PM on January 18, 2017 [2 favorites]


Best answer: You can also use a redirect match instead of rewrite. This will remove the .html from all URLs:
redirectMatch 301 ^(.*)\.html $1

Also- since you're moving your site, here is an awesome batch rewrite tool that I have been using for the last few years. Comes in really handy if you have a bunch of redirects to do, you just put them in a list and it generates the rewrite rules for you.
posted by ap_classic at 3:01 PM on January 18, 2017


Response by poster: Sorry for the delay getting back to this - thanks for your help but I think WordPress is messing things up when I try to add any rules to the htaccess file. Then again I could be missing something completely obvious.
posted by macdara at 2:42 AM on January 22, 2017


Best answer: I took the liberty of looking for some of your urls... here's two:

OLD: http://macdaraconroy.com/micro/2016/12/how_to_enjoy_indy_wrestling.html

NEW: http://macdaraconroy.com/2016/12/how_to_enjoy_indy_wrestling/

To do this and be specific about it... here's what I've come up with:
RedirectMatch 301 ^/micro/([0-9]+)/([0-9]+)/(.*)\.html$ http://macdaraconroy.com/$1/$2/$3/
This looks for your specific pattern - of /micro/YYYY/mm/slug_misc.html and then replaces it with http://macdaraconroy.com/YYYY/mm/slug_misc/

Hopefully that'll work for you for all cases.
posted by artlung at 5:39 PM on January 27, 2017 [1 favorite]


Response by poster: Thanks artlung - seems like that RedirectMatch just worked a treat! (And apologies for the late reply, I didn't see your comment till last night, oops.)
posted by macdara at 3:13 AM on February 18, 2017 [1 favorite]


« Older Suddenly dumped by a friend--how to move on   |   Umbrella for Women's March Newer »
This thread is closed to new comments.