.htaccess - 删除.php有效,但会导致404

时间:2015-04-03 00:22:13

标签: apache .htaccess

我正在使用PHP开发API系统,但我不希望这些URL包含文件扩展名。因此,我创建了一个包含以下内容的.htaccess文件:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://xxxxx.com/api/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://xxxxx.com/api/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

打开http://xxxxx.com/api/some_call.php时,它会将我重定向到http://xxxxx.com/api/some_call,但我收到404错误的错误:

The requested URL /api/some_call was not found on this server.

我可以完全控制(root)服务器,因此欢迎任何建议。

提前致谢,

CrushedPixel

1 个答案:

答案 0 :(得分:1)

尝试添加条件

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

然后尝试在Options -Multiviews

上方添加RewriteEngine On