301 Redirect Help-Need Regular Expression for Permalink Day and Name

7 replies
I am using the Redirection plugin to do a 301 redirect. I deleted a post and started getting 404 errors, broken links because the post had been removed. I tried publishing a new post, with the same title etc and backdating the publish date hoping this would resolve the issue. No such luck.

Because this post had the day and name permalink structure I am getting a broken link 404 error for each date since the original post was published:
http://example,com/2011/13/07/postname
http://example,com/2011/14/07/postname
http://example,com/2011/15/07/postname and on and on and on over 60+ dates so far.

So I need to regular expression for the permalink structure so I can just to ONE 301 redirect for the post not one for each date. The plugin information indicates this can be done with regular expression and matching URLs:

Matching URLs

Matching a URL is a key part of Redirection and consists of a source URL. This URL must exactly match a URL that you want to redirect. For example, your site has the page:
http://yoursite.com/oldpage/that/needs/redirecting/ The source URL for this is: /oldpage/that/needs/redirecting/. The source does not require your website address, and it is only possible to redirect a URL that exists on your website (you cannot redirect an external website, for example).
Advanced users can make use of regular expressions to reduce the number of redirections they need to create. A regular expression is basically a pattern that tells the plugin how to match. For example:
/(\d*)/(\d*)/(.*) This pattern tells the plugin that you want to match a URL that looks like:
/2007/05/some-url/ That is, the (d*) indicates a number, and the (.*) a sequence of characters. Regular expressions are a complicated subject and this page will not attempt to give more than a passing overview of using them. If you do need more help then you should take a look at a regular expression website.
Remember that if the source URL is a regular expression then you must enable the regular expression option, otherwise Redirection will just treat you source URL as plain text.
In addition to the source URL you can also specify a match condition:
  • URL only - Only matches the URL (the majority of your redirections will use this)
  • URL and referrer - Matches a URL when the source and referring site matches (i.e. match the URL only when the user came from a certain website)
  • URL and login status - Matches a URL when the source and user's login status matches (i.e. match the URL only when the user is logged in)
  • URL and user agent - Matches a URL when the source and user's browser matches (i.e. when the user is using a particular type of web browser)
These special rules can be both positive and negative. For example, you can match a URL when the user is logged in (and be redirect to one URL), or when the user is not logged in (and be redirected to another URL). This makes it very easy to create custom rules where users are redirected if they are using a particular browser, or if they came from a particular website.


If anyone could assist me with the I would really appreciate. My site is starting to take a hit in the rankings because the 404 errors are getting out of control.

Thanks in advance for your assistance,
Kind regards,
Shawn
#day #expression #permalink #regular
  • Profile picture of the author eminc
    Hi,

    A simple regular expression

    /[0-9]{4}/[0-9]{2}/([^/]+)

    works for a URL like

    http://example,com/2011/09/somepost

    I validated it using a online regex validator. If it doesn't work in your case let me know.

    Mohit
    Signature

    The best way to predict future is to create it ― Abraham Lincoln

    {{ DiscussionBoard.errors[4966940].message }}
    • Profile picture of the author airbusbarkley
      Originally Posted by Mohit Jawanjal View Post

      Hi,

      A simple regular expression

      /[0-9]{4}/[0-9]{2}/([^/]+)

      works for a URL like

      http://example,com/2011/09/somepost

      I validated it using a online regex validator. If it doesn't work in your case let me know.

      Mohit
      Mohit,

      Thanks! I'm off to give it a go. I'll let you know.

      Thanks again, your help is much appreciated.

      Shawn
      Signature
      Shawn
      {{ DiscussionBoard.errors[4967226].message }}
  • Profile picture of the author KabanaSoft
    it might be easier to just split the URL string into an array of strings and pull the last several elements of the array, rather than using regex's. Ex:

    $url = "http://example,com/2011/13/07/postname";
    $splitUrl = explode("/", $url); // Split the URL using the slash character
    $postName = $splitUrl[count($splitUrl) - 1]; // Should be 'postname'
    $postMonth = $splitUrl[count($splitUrl) - 2]; // Should be '07'
    $postDay = $splitUrl[count($splitUrl) - 3]; // Should be '13'

    Then you can construct your new redirect URL from these values...
    {{ DiscussionBoard.errors[4969328].message }}
  • Profile picture of the author eminc
    Looks like a nice idea if we are using PHP, but I guess the plugin which Shawn is using accepts only regular expression. Same with the case of a .htaccess file where we use mod_rewrite and write a regular expression to redirect the URLs. I suppose these are closely related.

    Mohit
    Signature

    The best way to predict future is to create it ― Abraham Lincoln

    {{ DiscussionBoard.errors[4970390].message }}
    • Profile picture of the author airbusbarkley
      Mohit,

      I tried the regular expression and it did not work. My post permalink structure has the full date, /2011/07/13 do I need to add something additional?

      Or maybe it is how I wrote it (I'm sure it could be an error on my part as this is pretty new to me):

      /[0-9]{4}/[0-9]{2}/([^/]+)/postname-goes-here



      The error message I received was something to the effect of the redirect was in such a way that the server would not be able to find it/deliver it etc.

      Once again, I really appreciate your assistance.

      Kind regards,
      Shawn
      Signature
      Shawn
      {{ DiscussionBoard.errors[4974757].message }}
      • Profile picture of the author eminc
        Originally Posted by airbusbarkley View Post

        Mohit,

        I tried the regular expression and it did not work. My post permalink structure has the full date, /2011/07/13 do I need to add something additional?

        Or maybe it is how I wrote it (I'm sure it could be an error on my part as this is pretty new to me):

        /[0-9]{4}/[0-9]{2}/([^/]+)/postname-goes-here



        The error message I received was something to the effect of the redirect was in such a way that the server would not be able to find it/deliver it etc.

        Once again, I really appreciate your assistance.

        Kind regards,
        Shawn
        Hi Shawn,

        The regular expression is complete in a way, and includes the postname. So you don't need to add another post name to it. I will break the code for you:

        [0-9]{4} - A 4 digit number (i.e. year)
        [0-9]{2} - A 2 digit number (i.e. month)
        ([^/]+) - Any combination of letters and numbers (i.e. postname)

        So I think you only need this /[0-9]{4}/[0-9]{2}/([^/]+) to translate to /2011/09/postname-here .

        I recommend that you add only /[0-9]{4}/[0-9]{2}/([^/]+)only and let me know if it works. You can message me on skype(mjawanjal) and we can quickly discuss the other possibilities also.

        Mohit
        Signature

        The best way to predict future is to create it ― Abraham Lincoln

        {{ DiscussionBoard.errors[4977936].message }}
        • Profile picture of the author airbusbarkley
          Mohit,

          Thanks for the breakdown of the regular expression code. I adjusted it and added the postname that I needed to re-direct and it worked great.

          Once again, I really appreciate your assistance.

          Shawn
          Signature
          Shawn
          {{ DiscussionBoard.errors[4981340].message }}

Trending Topics