How do you make an exception to a .htaccess rewrite rule?

Viewed 68

I am rewriting using .htaccess to remove ".html" extensions from my website. However, I use the URL in a function to pass a parameter to another page (mysite.com/locations#12345)

My rewrite rules are the following:

RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

I tried doing this:

RewriteCond %{THE_REQUEST} /locations/([^.]+) [NC]
RewriteRule ^ /locations%1 [NC,L,R]

But I get "mysite.com/locations%20HTTP/2#12345"

1 Answers

You need to turn off DirectorySlash to turn off trailing slash for directories moreover turn off Indexes to avoid listing directory content.

DirectorySlash Off
Options +FollowSymLinks -MultiViews -Indexes

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+([^.]+)\.html[?\s] [NC]
RewriteRule ^ /%1 [L,R=302]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Make sure to test it after clearing browser cache.

Related