1

I am using the following permalink structure:

/%postname%-%monthnum%%year%%post_id%.html

I want to get it changed to just /%postname%/.

Please help me with what should be the correct redirect rule for .htaccess.

2
  • Have you already changed the permalink structure in WordPress? (Although it doesn't sound like you have?) You don't change the permalink structure by just creating a redirect in .htaccess. (?)
    – MrWhite
    Commented May 8, 2018 at 9:58
  • 1
    No I have not. After making change in wordpress, I need to redirect because automatically it wont work. Please help.
    – Skotlive
    Commented May 8, 2018 at 11:45

2 Answers 2

1

Assuming you have changed your permalinks in WordPress (under Settings > Permalinks), as mentioned earlier, then you can redirect your old URLs en masse (in order to preserve SEO) with something like the following at the top of your .htaccess file, using mod_rewrite:

RewriteRule ^([\w-]+)-\d{7,}\.html$ /$1/ [R=302,L]

This will redirect URLs of the form /%postname%-%monthnum%%year%%post_id%.html to /%postname%/, assuming your post name is limited to the characters A-Z, a-z, 0-9, _, -.

As noted above, this must go before your WordPress front-controller (ie. before the # BEGIN WordPress section), at the top of your .htaccess file.

Note also, that this is currently a temporary (302) redirect. Change the 302 to 301 (permanent) only once you have tested that this is working OK. This is to avoid any problems with browser caching.

0
0

So basically you want to change the URL structure and then redirect the old URLs to the new ones?

As noted by MrWhite, you change the premalink structure in WordPress, under Settings->Permalinks.

Once you have changed them, you can redirect the old URLs one by one by adding this command and using your real URLs:

RedirectPermanent /old-file.html http://www.domain.com/new-file

You can either do this in .htaccess or in your control panel on the server, if you have such.

1
  • "redirect the old URLs one by one" - You don't need to redirect the old URLs "one by one". You can match the URL pattern in a single directive (see my answer). Also, it is not recommended to mix mod_alias Redirect and mod_rewrite in a single config file - since the mod_rewrite directives will execute first, regardless of the apparent order of directives in the file (which can result in some unexpected conflicts).
    – MrWhite
    Commented May 8, 2018 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.