1

I recently changed the permalink structure on my 2-month-old blog to a user friendly one which means a whole bunch of 404s. I wish to redirect the following:

https://example.com/poastname.html

to

https://example.com/postname/

Before coming here, I spent days looking for this redirect online but never found. The redirect plugins recommended don't work. I just need a .htaccess rule to fix this.

0

Try adding this code to .htaccess:

RedirectMatch 404 /\d{4}/\d{2}/\d{2}/(.*)\.html http://example.com/$1

It should fix the issue.

  • Your regex doesn’t match the example URL in the question. However, since WP already uses mod_rewrite for its front-controller, you should also use mod_rewrite for this redirect. – MrWhite Dec 15 '18 at 0:59
  • @mrwhite Can you please elaborate? Or better still, please rewrite the rule entirely. I'm a novice to Wordpress and when I imputed that rule into my .htaccess file it broke the site. I put the rule one space below the #End Wordpress – Eze King Eke Dec 16 '18 at 16:47
  • @EzeKingEke I've added an answer. (However, the directive above shouldn't have "broken your site"?! ...shouldn't have done anything at all?) – MrWhite Dec 16 '18 at 23:08
0

You would need to do something like the following at the top of your .htaccess file, before the existing front-controller (ie. before # BEGIN WordPress).

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

This assumes your "postname" consists of just the characters a-z, A-Z, 0-9, _ and -.

The RewriteRule pattern captures the postname before the .html URL suffix and saves this in the $1 backreference, which is used in the substitution string (2nd argument).

Note that this is a 302 (temporary) redirect. Only change it to 301 (permanent) when you are sure it's work OK, in order to avoid any potential caching issues. As always, clear your browser cache before testing.

You should use mod_rewrite (as opposed to mod_alias Redirect or RedirectMatch) in order to avoid conflicts with existing mod_rewrite directives. mod_rewrite executes before mod_alias, despite the apparent order of the directives in the config file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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