WordPress.org

Ready to get started?Download WordPress

Forums

mod_rewrite / htaccess question (7 posts)

  1. dmiyares
    Member
    Posted 2 years ago #

    Hi:

    Hoping someone with better knowledge of mod_rewrite will find this question simple. I've taken over duties of a website for a company and am trying to do some SEO on it. The permalink structure was
    %year%/%month%/%post_id%/%category%/%postname%
    when I took control and I've changed it to simply %postname%. Everything on the site works fine, however, Google has over 1000 links point to the old urls
    http://www.example.com/2011/10/7639/cateogry/post_name/
    which throw 404 errors. I've tried a couple of plugins to fix the problem but they didn't work so I'm trying to learn my way around mod_rewrite to fix this.

    I've tried (and I've not optimized the regex just so I can read it better)
    ^/([0-9])([0-9])([0-9])([0-9])/([0-9])([0-9])/([0-9])([0-9])([0-9])([0-9])/(.*?)/(.*?) /$5 [L]

    However, this still hits a 404 page.

    Any pointers would be great ... thanks!

  2. I would have thought canonical URLs would have taken care of that, but you're getting 404s back so it's not.

    If I understand it correctly, you want this

    http://www.example.com/2011/10/7639/cateogry/post_name/

    to become just this

    http://www.example.com/post_name/

    Or put differently

    http://www.example.com/$1/$2/$3/$4/$5/

    becomes

    http://www.example.com/$5/

    If so, try this above your WordPress rules in .htaccess

    # Old URLs to new
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    Rewriterule ^(.*)/(.*)/(.*)/(.*)/(.*)/ http://your-blog-url/$5/ [L]
    </IfModule>
    
    # BEGIN WordPress

    See if that works for you.

  3. I forgot this part, if that does work put the response code 301 into that rewrite rule like so

    Rewriterule ^(.*)/(.*)/(.*)/(.*)/(.*)/ http://your-blog-url/$5/ [R=301,L]

    That will tell the search engines that the URLs are permanently moved to the new location.

  4. http://www.example.com/2011/10/7639/category/post_name/ would be easier if you locked down the YYYY/MM slugs first...

    My first shot would be this:

    RewriteRule ^([0-9]{4})/([0-9]{2})/(.*)/category/(.*)$ http://domain.com/$4 [L,R=301]

  5. Okay, after a sidebar conversation, this seems to be it:

    # Old URLs to new
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9].*)/(.*)/(.*)$ http://your-blog-url/$4/ [L,R=301]
    </IfModule>
    
    # BEGIN WordPress

    The original one I posted breaks things like the visual editor. This one is more specific with the ####/##/numbers-here/category/slug pattern.

  6. dmiyares
    Member
    Posted 2 years ago #

    Very cool.. thanks everyone... :)

  7. cmegown
    Member
    Posted 2 years ago #

    I couldn't tell you an exact name, but I'm 99% sure there is a plugin for that. "Redirection" comes to mind, but I didn't go off and research it.

    I assume there is a plugin for it because you aren't exactly the first person to ever change their permalink structure after a large post base has been built, so I would make sure to do some research on that.

Topic Closed

This topic has been closed to new replies.

About this Topic