Convert .html to -html in file name

I converted my website to WordPress. In the process, in order to satisfy WordPress’s rules about permalink formats, I had to change the filenames from ending in .html to ending in -html.

Unfortunately, there are backlinks from other sites that point to the old pages that end in .html. So when someone clicks one of those backlinks, the page cannot be found on the new site.

Read More

For this reason, I want to permanently redirect all requests for URIs ending in .html using .htaccess so that they end instead with -html.

So for instance:

https://example.com/file1.html

needs to permanently redirect to

https://example.com/file1-html

Sadly, my code generates 500 errors.

Here’s what I wrote based upon what I found in the Apache manual:

<Directory /home/accountname/public_html>
    RewriteEngine on
    RewriteBase /home/accountname/public_html

    RewriteCond $1.html !-f
    RewriteRule ^(.*).html$ $1-html [R=301,L]
</Directory>

Can someone help?

Related posts

Leave a Reply

1 comment

  1. <Directory> directive isn’t allowed in .htaccess.

    Place this rule in your main WP .htaccess:

    RewriteEngine on
    
    RewriteRule ^(.+?).html$ /$1-html [R=301,L,NE,NC]
    

    Make sure this is first rule below RewriteEngine on line.