TransWikia.com

redirect/ %category%/%postname%.html to /%category%/%postname%

WordPress Development Asked by quantaws on December 17, 2021

My site was first built with html/css in 2000, and when I rebuilt it with WordPress in 2012, I kept the .html extension in the interest of keeping the same URLs. I’d like to get rid of it now and redirect the old html pages to the new extension-less pages.

My current permalink structure is:
/%category%/%postname%.html

I’d like it to be:
/%category%/%postname%

To do that, I tried deleting “.html’ from the end of the permalink structure, then saving permalinks. Then, I put this in my .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

But it doesn’t work — pages just give me 404 errors that way.

I took a look at the Yoast permalink redirection tool, but that only redirects to /%postname% — I’m not sure if it’s possible to modify the outputted rule to add the category before the postname.

2 Answers

OK. I finally got it done. Here's what I did: (1.) changed the WordPress permalink structure to /%category%/%postname% and saved.

(2.) Added this to .htaccess: RewriteRule (.+).html?$ https://example.com/$1 [R=301,L] (The rewrite rule in my original question didn't work for me.)

Now the .html URLs are redirecting to the new extensionless URLs.

Answered by quantaws on December 17, 2021

It sounds like you didn't flush your permalinks. Remove what you wrote in your .htaccess. Set what you had that was working before in the custom section of the permalink settings %category%/%postname%.html, save it, select 'plain', save it, then change it to your new settings %category%/%postname% save it, select 'plain', save it, then go to your custom setting and it should work.

Answered by Gregory Schultz on December 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP