从url中删除.php [重复]

This question already has an answer here:

Well, I know that it should be done by .htaccess my .htaccess is in the root of my site and here it's content

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Also mod rewrite is enabled at my server. I have a form with an action on ./getReadings.php in it. But when I change it to ./getReadings it says that tehre isn't such file on server. What's my mistake?

</div>

Try adding Options -MultiViews before RewriteEngine directive

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]

Note: MultiViews is about Apache content negociation (which is the problem here since filename will automatically be translated as filename.php existing file). That's why you have to disable it.


EDIT: right now, you can access same content by 2 different urls (with or without php extension). To avoid duplicate content (which is bad for search engines) you can redirect extensions to extensionless equivalent with this code

Options -MultiViews
RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
    RewriteEngine on 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php