WordPress.org

Support

Support » How-To and Troubleshooting » mod_rewrite / htaccess question

mod_rewrite / htaccess question

  • 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!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Jan Dembowski

    @jdembowski

    Brute Squad and Volunteer Moderator

    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.

    Moderator Jan Dembowski

    @jdembowski

    Brute Squad and Volunteer Moderator

    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.

    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]

    Moderator Jan Dembowski

    @jdembowski

    Brute Squad and Volunteer Moderator

    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.

    Very cool.. thanks everyone… 🙂

    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘mod_rewrite / htaccess question’ is closed to new replies.
Skip to toolbar