{"id":906,"date":"2008-12-15T13:13:00","date_gmt":"2008-12-15T05:13:00","guid":{"rendered":"http:\/\/www.nargalzius.com\/blog\/archives\/2008\/12\/15\/why-didnt-i-think-of-this-sooner\/"},"modified":"2008-12-15T13:13:00","modified_gmt":"2008-12-15T05:13:00","slug":"why-didnt-i-think-of-this-sooner","status":"publish","type":"post","link":"http:\/\/nargalzius.com\/blog\/archives\/2008\/12\/15\/why-didnt-i-think-of-this-sooner","title":{"rendered":"Why didn&#8217;t I think of this sooner!?"},"content":{"rendered":"<p>So I was giving myself a hard time by <a href=\"http:\/\/farm4.static.flickr.com\/3054\/3082071733_c70f58873d_b.jpg\">manually adding<\/a> <code>.htaccess<\/code> match conditions whenever there was a <code>404<\/code> (file not found) error reported <span class=\"footnote_referrer\"><a role=\"button\" tabindex=\"0\" onclick=\"footnote_moveToReference_906_1('footnote_plugin_reference_906_1_1');\" onkeypress=\"footnote_moveToReference_906_1('footnote_plugin_reference_906_1_1');\" ><sup id=\"footnote_plugin_tooltip_906_1_1\" class=\"footnote_plugin_tooltip_text\">1 <\/sup><\/a><span id=\"footnote_plugin_tooltip_text_906_1_1\" class=\"footnote_tooltip\">which was related to the old permalink formats I used in my MovableType days.<\/span><\/span><script type=\"text\/javascript\"> jQuery('#footnote_plugin_tooltip_906_1_1').tooltip({ tip: '#footnote_plugin_tooltip_text_906_1_1', tipClass: 'footnote_tooltip', effect: 'fade', predelay: 0, fadeInSpeed: 200, delay: 400, fadeOutSpeed: 200, position: 'top right', relative: true, offset: [10, 10], });<\/script><\/p>\n<p>I realized just how much of an idiot I was when I could&#8217;ve used regular expressions. As such, I was able to replace <em>all of them<\/em> with just these <strong>five<\/strong> lines of code:<\/p>\n<pre><code>RedirectMatch permanent ^\/blog\/archives\/(.*)\/([0-9]{4})_([0-9]{2})_([0-9]{2})(.*)\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\/$3\/$4\nRedirectMatch permanent ^\/blog\/archives\/(.*)\/index_([0-9]{4})\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\nRedirectMatch permanent ^\/blog\/archives\/(.*)\/index_([0-9]{4})_([0-9]{2})\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\/$3\nRedirectMatch permanent ^\/blog\/archives\/(.*)\/index_([0-9]{4})_([0-9]{2})_([0-9]{2})\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\/$3\/$4\nRedirectMatch permanent ^\/blog\/archives\/([a-z]{1,})($|\/$) http:\/\/nargalzius.com\/blog\/archives\/category\/$1<\/code><\/pre>\n<p>Let&#8217;s break them down shall we?<!--more--><!--\/\/--><\/p>\n<h1>Individual Permalinks<\/h1>\n<pre><code>RedirectMatch permanent ^\/blog\/archives\/(.*)\/([0-9]{4})_([0-9]{2})_([0-9]{2})(.*)\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\/$3\/$4<\/code><\/pre>\n<p>Basically is a match string for my old individual entry permalink which is in the format:<\/p>\n<p><code>http:\/\/www.nargalzius.com\/blog\/archives\/YYYY\/MM\/YYYY_MM_DD_HHMMXX.php<\/code> Where <code>XX<\/code> is <code>AM<\/code> or <code>PM<\/code><\/p>\n<p>It <em>utlimately<\/em> checks the <strong>last part<\/strong> of the <code>URL<\/code> and checks if it&#8217;s got a &#8220;date grouping&#8221; (YYYY_MM_DD) which are a specific number of positive integers separated by underscores. It then disregards the existence <code>AM<\/code>\/<code>PM<\/code> and makes sure it ends with <code>.php<\/code>. If it matches, then we&#8217;ve got ourselves an old permalink format, and it will proceed to do the voodoo it was intended to do.<\/p>\n<p>The only change with regards to the redirect behaviour of this &#8220;new&#8221; way is that since there&#8217;s no way of &#8220;factoring&#8221; the AM\/PM to get to an individual entry&#8230; it&#8217;s going to redirect to the <em>date-based<\/em> archive.<\/p>\n<p>For example, with the old manual method, clicking on this link: <a href=\"http:\/\/www.nargalzius.com\/blog\/archives\/2008\/07\/2008_07_31_1506PM.php\">http:\/\/www.nargalzius.com\/blog\/archives\/2008\/07\/2008_07_31_1506PM.php<\/a> would&#8217;ve taken you <strong>directly<\/strong> to <a href=\"http:\/\/www.nargalzius.com\/blog\/archives\/2008\/07\/31\/zoom-just-one-look\">this page<\/a>. That obviously isn&#8217;t the case anymore. But you can still see the same post registered in the date-based archive. So overall, even with the new way, it should show enough information for the redirected user to click on which &#8220;post&#8221; he\/she probably was looking for.<\/p>\n<p>The best thing about this is that it&#8217;s kinda &#8220;forward thinking&#8221; as far as anticipating <code>404<\/code>s. I didn&#8217;t have time to sit down and map out <strong>all<\/strong> my entries to the new <code>URL<\/code>s, so I would just wait until I got a <code>404<\/code> report that <strong>wasn&#8217;t<\/strong> from a search engine spider\/bot. At least with this approach, it covers <em>everything<\/em> &#8211; pretty effectively at that.<\/p>\n<h1>Date-based Archives<\/h1>\n<p>We then got provisions for the date index pages. <\/p>\n<pre><code>RedirectMatch permanent ^\/blog\/archives\/(.*)\/index_([0-9]{4})\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\nRedirectMatch permanent ^\/blog\/archives\/(.*)\/index_([0-9]{4})_([0-9]{2})\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\/$3\nRedirectMatch permanent ^\/blog\/archives\/(.*)\/index_([0-9]{4})_([0-9]{2})_([0-9]{2})\\.php$ http:\/\/nargalzius.com\/blog\/archives\/$2\/$3\/$4<\/code><\/pre>\n<p>The old system referenced flat files as far as general date based archives went. They were similar to the individual entries &#8211; only they didn&#8217;t have any time information&#8230; and they were all preceded by <code>index_<\/code><\/p>\n<p>The three lines simply match the [three] possible cases in which the user might&#8217;ve stumbled upon a date-based archive in the old permalink format; namely a yearly, monthly, or daily archive. Then it would and redirect accordingly.<\/p>\n<h1>Category Archives<\/h1>\n<p>As for the code below:<\/p>\n<pre><code>RedirectMatch permanent ^\/blog\/archives\/([a-z]{1,})($|\/$) http:\/\/nargalzius.com\/blog\/archives\/category\/$1<\/code><\/pre>\n<p>The old permalink sructure for category pages was <code>\/blog\/archives\/category_name<\/code>. For some reason, the WP people decided to make it difficult to retain such a structure without horrible side-effects. So you essentially have to put them in a child directory for it to work properly (e.g. \/blog\/archives\/<strong>category<\/strong>\/category_name).<\/p>\n<p>The code we just mentioned matches the old permalink, by checking if a <em>single<\/em> word comprised of letters only finishes the <code>URL<\/code> &#8211; which basically indicates that it was probably a category archive. Then redirects it accordingly.<\/p><div class=\"speaker-mute footnotes_reference_container\"> <div class=\"footnote_container_prepare\"><p><span role=\"button\" tabindex=\"0\" class=\"footnote_reference_container_label pointer\" onclick=\"footnote_expand_collapse_reference_container_906_1();\">Notes<\/span><span role=\"button\" tabindex=\"0\" class=\"footnote_reference_container_collapse_button\" style=\"display: none;\" onclick=\"footnote_expand_collapse_reference_container_906_1();\">[<a id=\"footnote_reference_container_collapse_button_906_1\">+<\/a>]<\/span><\/p><\/div> <div id=\"footnote_references_container_906_1\" style=\"\"><table class=\"footnotes_table footnote-reference-container\"><caption class=\"accessibility\">Notes<\/caption> <tbody> \r\n\r\n<tr class=\"footnotes_plugin_reference_row\"> <th scope=\"row\" class=\"footnote_plugin_index_combi pointer\"  onclick=\"footnote_moveToAnchor_906_1('footnote_plugin_tooltip_906_1_1');\"><a id=\"footnote_plugin_reference_906_1_1\" class=\"footnote_backlink\"><span class=\"footnote_index_arrow\">&#8673;<\/span>1<\/a><\/th> <td class=\"footnote_plugin_text\">which was related to the old permalink formats I used in my MovableType days.<\/td><\/tr>\r\n\r\n <\/tbody> <\/table> <\/div><\/div><script type=\"text\/javascript\"> function footnote_expand_reference_container_906_1() { jQuery('#footnote_references_container_906_1').show(); jQuery('#footnote_reference_container_collapse_button_906_1').text('\u2212'); } function footnote_collapse_reference_container_906_1() { jQuery('#footnote_references_container_906_1').hide(); jQuery('#footnote_reference_container_collapse_button_906_1').text('+'); } function footnote_expand_collapse_reference_container_906_1() { if (jQuery('#footnote_references_container_906_1').is(':hidden')) { footnote_expand_reference_container_906_1(); } else { footnote_collapse_reference_container_906_1(); } } function footnote_moveToReference_906_1(p_str_TargetID) { footnote_expand_reference_container_906_1(); var l_obj_Target = jQuery('#' + p_str_TargetID); if (l_obj_Target.length) { jQuery( 'html, body' ).delay( 0 ); jQuery('html, body').animate({ scrollTop: l_obj_Target.offset().top - window.innerHeight * 0.2 }, 380); } } function footnote_moveToAnchor_906_1(p_str_TargetID) { footnote_expand_reference_container_906_1(); var l_obj_Target = jQuery('#' + p_str_TargetID); if (l_obj_Target.length) { jQuery( 'html, body' ).delay( 0 ); jQuery('html, body').animate({ scrollTop: l_obj_Target.offset().top - window.innerHeight * 0.2 }, 380); } }<\/script>","protected":false},"excerpt":{"rendered":"<p>So I was giving myself a hard time by manually adding .htaccess match conditions whenever there was a 404 (file not found) error reported 1 which was related to the old permalink formats I used in my MovableType days. I realized just how much of an idiot I was when I could&#8217;ve used regular expressions. &hellip; <p class=\"link-more\"><a href=\"http:\/\/nargalzius.com\/blog\/archives\/2008\/12\/15\/why-didnt-i-think-of-this-sooner\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Why didn&#8217;t I think of this sooner!?&#8221;<\/span><\/a><\/p><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[5,9,13],"tags":[565,595,810,811,1000,1001,1274,1280,1282],"class_list":["post-906","post","type-post","status-publish","format-standard","hentry","category-internet","category-nargalzius","category-technology","tag-htaccess","tag-internet","tag-nargalzius","tag-nargalziuscom","tag-regex","tag-regular-expressions","tag-web","tag-website","tag-webstuff"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/posts\/906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/comments?post=906"}],"version-history":[{"count":0,"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/posts\/906\/revisions"}],"wp:attachment":[{"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/media?parent=906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/categories?post=906"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nargalzius.com\/blog\/wp-json\/wp\/v2\/tags?post=906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}