嗯,我知道它应该由 .htaccess 完成 我的 .htaccess 位于我网站的根目录中,这里是内容
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
我的服务器还启用了 mod 重写。
我有一个表单,其操作为 ./getReadings.php
在里面。
但是当我将其更改为./getReadings
时它说服务器上没有这样的文件。我的错误是什么?
最佳答案
尝试在 RewriteEngine
指令之前添加 Options -MultiViews
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
注意:MultiViews
与 Apache 内容协商有关(这是这里的问题,因为 filename
将自动翻译为 filename.php
现有文件)。这就是为什么你必须禁用它。
编辑:现在,您可以通过 2 个不同的 url 访问相同的内容(带或不带 php
扩展名)。为了避免重复内容(这对搜索引擎不利),您可以使用此代码将扩展重定向到无扩展等效内容
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]
关于php - 从 url 中删除 .php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26097039/