lucy24 - 5:37 pm on Apr 14, 2012 (gmt 0)
By weird coincidence, I have just this second (really) tested what happens if I stick a bogus parameter onto the name of a random html page--which, afaik, shouldn't even have parameters. The page is served up exactly as normal. But further checking confirms that if I continue after the .html -- that is, as if the URL simply wasn't finished yet-- I get the desired 404.
Happily you don't need to make an exception for query strings, because mod_rewrite ignores them by default. That is, it stashes the query in a safe place, executes the rule and reattaches the query unchanged-- unless you have specifically told it to do otherwise.
Now, a normal URL only contains one full stop: the one right before the extension. (Only yesterday there was a question elsewhere in this Forum about filenames with multiple full stops, like .txt.zip. Can we assume you don't have anything like that?)
RewriteRule ^([^.]+\.[^/.]+)[/.] http://www.example.com/$1 [R=301,L]
Beginning anchor is crucial; ending anchor isn't needed. "Capture everything up to the first period (full stop). Capture the full stop. Capture anything after the . that is neither a . nor a directory slash. If you meet another full stop or a directory slash, stop and redirect."
It doesn't matter what, if anything, comes after that first unwanted . or / It only matters that there's something after the extension. If there's nothing-- or if the request doesn't contain an extension in the first place-- the rule will simply not execute.
If the problem is limited to php files-- or some other specific extension-- narrow it down to
RewriteRule ^([^.]+\.php)[/.] http://www.example.com/$1 [R=301,L]
If people start hitting you with garbage extensions like .2az, more fine-tuning is in order. But if you made the server check for everything that could possibly be requested,* you'd never get out of htaccess alive.
:: quick detour here to verify that my own htaccess doesn't have a closing anchor after ".php" (blocked because I don't use them) ::
* No need for a million monkeys. A single cat walking across the keyboard will do nicely.