301 Redirect all page and post urls from .html to /

I have a current site where all URLs end with .html.

I created a new site and the URLs are pretty much the same but without .html.

I've been trying codes found here in my .htaccess file and most seem to cause Internal Server Error.

http://example.com/page1.html to http://example.com/page1/
http://example.com/page1/page2.html to http://example.com/page1/page2/

My current .htaccess file code is:

# BEGIN WordPress
IfModule mod_rewrite.c
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
/IfModule

# END WordPress

Topic htaccess redirect Wordpress

Category Web


Just to note, when you get an Internal Server Error (code 500), you should check your server's error log for the specifics of the error. If you've been messing with mod_rewrite in .htaccess then this could be anything from a basic syntax error to a rewrite loop.

If you have no .html files on your new site then you can issue an unconditional redirect to remove the .html on the end of the URL. For example:

RewriteRule (.*)\.html$ /$1 [R=301,L]

This would need to go before the existing WordPress directives.

Or, if you do have some .html files that need to be served as-is then only redirect requests that do not map to an existing file. For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)\.html$ /$1 [R=301,L]

You will need to clear your browser cache before testing.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.