Htaccess - replace .html with a forward slash

.htaccess, apache, mod-rewrite, redirect, regex

Solution

Enable mod_rewrite and .htaccess through `httpd.conf` and then put this code in your `.htaccess` under `DOCUMENT_ROOT` directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]

# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

Problem

I want to redirect URLs of this form: `/page.html?variable=value&othervar=true&thirdvar=100` To this: `/page/?variable=value&othervar=true&thirdvar=100` So basically I just want to replace the `.html` in the middle of the `URL` with a `forward slash`, but I need to preserve the get string that comes with it. This is what I tried: RewriteRule ^page.html(.+)$ /page/$1 [L,R=301] But this doesn't appear to be working for me. I've made similar things work recently but I can't figure out what I'm missing here. Thanks for any input.

Original source