I found this script or removing extensions from php files:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
While this works, it still allows files to be accessed if you type them in with their extension, which I don't want.
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([.]+)$ $1.php [NC,L]
I also tried ^(.*)$
and ^(.+)$
which seems like it should do the job, because it would do this:
index.php -> index.php.php
but somehow, it doesn't work as expected.
So how do I update the above .htaccess script to disallow file extensions?
EDIT:
My .htaccess script seems to be detecting the files correctly:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "other2.html" [L]
The pages other.html and other2.html simply contain the words "OTHER" and "OTHER 2"
Now, using the above script, the output is as expected:
"/test.php" gives output "OTHER"
"/test" gives "OTHER 2"
but if I update the script to the following, both url variations start returning "OTHER"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "other.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L] // changed
So it seems that after the extensionless filename has ".php" added to it by rule#2, it somehow gets caught by rule #1.
Aren't these rules ordered? and isn't [L] supposed to stop processing on a match?
EDIT 2:
So assuming [L]
did what I was expecting... the following script would work...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule "^.*\.php$" "404.html" [L]
RewriteRule "^([^\.]+)$" "$1.php" [L]