I'd like to redirect all .htm
documents in the root (i.e. www.example.com/page.htm
) to remove the extension (i.e. www.example.com/page/
) but not redirect .htm
documents in sub-directories (i.e. www.example.com/subdir/page.htm
).
How is that done in .htaccess
?
CodePudding user response:
Try something like the following at the top of the root .htaccess
file, before the existing WordPress directives (ie. before # BEGIN WordPress
).
# Redirect "/<page>.htm" to "/<page>/"
RewriteRule ^([^./] )\.htm$ /$1/ [R=302,L]
You do not need to repeat the RewriteEngine
directive, as this already occurs later in the WordPress directives. (The order of the RewriteEngine
directive does not matter - the last instance "wins".)
This removes the .htm
extension from any requested URL in the root only and appends a trailing slash (as in your example). This also assumes <page>
itself does not contain a dot.
Note that this is a 302 (temporary) redirect. If this is intended to be permanent then change it to a 301, but only after you have confirmed that it works (to avoid potential caching issues).