WordPress.org

Support

Support » How-To and Troubleshooting » need a redirect rule to send old permalink stucture to new one

need a redirect rule to send old permalink stucture to new one

  • Greetings
    Years ago, I set my custom permalinks to:
    /%postname%.html
    And now that I know this was a bad choice, I tried to change it to: /%category%/%postname%
    But the problem is that the links in google now give 404 error pages.
    Is there a code I can add to my htaccess to redirect links from the old strucutre to the new one please?

    Currently, my htaccess has this:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress

    Many thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Put this before the WordPress bit:

    <IfModule mod_rewrite.c>
      # Always use www.
      RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
      RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
    
      # Change urlpath.html to urlpath
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
      RewriteRule ^(.*)\.html$ http://www.example.com/$1 [L,R=301]
    </IfModule>

    I’ve used this before and it works. You can probably get away with removing the www forcing, but I’m not sure and this way it’ll always be consistent.

    Thanks for the code.
    Is there a code that doesn’t involve the www bit? I don’t use them as my blog is in a subdomain.
    what about a code to redirect
    from: /%postname%.html
    to: /%post_id%/%postname%

    Then just this may work

    <IfModule mod_rewrite.c>
      # Change urlpath.html to urlpath
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
      RewriteRule ^(.*)\.html$ http://subdomain.example.com/$1 [L,R=301]
    </IfModule>

    The forcing the www was to make sure it was consistent, but if you’re on a subdomain, it shouldn’t be needed anymore.

    Thanks, but your code didn’t work in my case.
    But thanks for trying to help.

    any other help please?

    Hmm. Okay, what I gave would translate example.com/foobar.html to example.com/foobar

    For postID/foobar though … I don’t know how you can move those, since the ID isn’t detectable from just the URL.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘need a redirect rule to send old permalink stucture to new one’ is closed to new replies.