How to Easily Change Your WordPress Blog URLs Using .htaccess

Posted on the 25 May 2024 by Techcanada

To change the URLs of all blog posts from abc.com to abc.com/blog/url using the .htaccess file, you can use mod_rewrite rules. Here’s how you can do it:

  1. Access Your .htaccess File:
    • This file is usually located in the root directory of your WordPress installation.
  2. Edit the .htaccess File:
    • Add the following code to your .htaccess file. Make sure to place it before any WordPress-specific rules (which are usually enclosed within # BEGIN WordPress and # END WordPress comments).
apacheCopy codeRewriteEngine On
RewriteBase /

# Redirect old post URLs to new URLs with /blog/ prefix
RewriteRule ^post/(.*)$ /blog/$1 [R=301,NC,L]
  1. Save the .htaccess File:
    • Save the changes and upload the file back to your server if you edited it locally.

Explanation:

  • RewriteEngine On: Enables the rewrite engine.
  • RewriteBase /: Sets the base URL for the rewrites.
  • RewriteRule ^post/(.*)$ /blog/$1 [R=301,NC,L]:
    • This rule matches any URL that starts with post/ followed by any characters ((.*)).
    • It captures the part after post/ and appends it to /blog/, creating the new URL.
    • R=301: Sends a 301 Moved Permanently response to ensure search engines update their links.
    • NC: Case-insensitive match.
    • L: Indicates this is the last rule to be processed if the rule matches.

Important Notes:

  • Make sure you have a backup of your .htaccess file before making any changes.
  • If your existing URLs follow a different structure, you may need to adjust the RewriteRule accordingly.
  • After making these changes, clear your browser cache and any caching mechanisms you may be using on your WordPress site to see the changes.

By following these steps, you should be able to redirect all your old blog post URLs to the new structure without issues.