RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [L,R=301]
The regex ^(.+)/$
only matches URLs that end with a slash, so it matches /pagename.html/
, but not /pagename.html/randomchars
.
However, to avoid matching the slash anywhere in the URL (you probably have multiple path segments in some of your URLs) you can specifically match the file extension .html
. This also negates the need for the directory check.
For example, try the following instead:
RewriteRule ^(.+?\.html)/ /$1 [R=302,L]
Note the absence of the trailing end-of-string anchor ($
) on the regex.
I made the regex non-greedy (ie. the ?
in .+?
) so that it matches the first instance of .html
in the URL-path and not the last. Otherwise, if the "randomchars" also contained an instance of .html/
then you'd potentially get just another broken request/redirect.
Test first with a 302 (temporary) redirect to avoid potential caching issues. Only use a 301 (permanent) redirect when you are sure everything is working OK.
I'm not really sure how or why these broken URLs are rendering at all.
If pagename.html
is a real file then the trailing /randomchars
are additional pathname information (path-info) on the URL. Whether Apache accepts path-info on the URL or whether it triggers a 404 is (by default) dependent on the handler that manages the file type (.html
, .php
, etc.). Although the handler for .html
files usually rejects path-info by default, unless you've explicitly enabled it or are parsing .html
files as PHP or something?
If this is indeed path-info (as described above) then you could instead disable path-info for all file types:
AcceptPathInfo Off
If you then remove the redirect (RewriteRule
directive) above then such URLs will generate a 404 as intended. If you keep the redirect then the redirect will still occur.
with that malformed URL, part of the page renders, but renders with broken styling for some reason.
The broken styling (failure to load CSS) is most probably due to using relative URL-paths to your CSS files. With the additional path segment(s) on the URL (ie. path-info), you are changing the base URL that the relative URL is relative to. eg. A relative URL of the form css/mystyles.css
will resolve to /pagename.html/css/mystyles.css
instead of /css/mystyles.css
as is probably intended, so results in a 404 and no styles are rendered. See my answer to the following question for more information on this: .htaccess rewrite URL leads to missing CSS
/pagename.html
" - ispagename.html
a real file? Does it really have a.html
file extension?