NGINX/SWAG:重写 URL 以删除 .php 扩展名

NGINX/SWAG: Rewrite URL to remove .php extension

互联网上有很多类似的问题(Whosebug 和其他网站),但其中 none 对我有用。 我正在尝试重写我的 URL 以去掉末尾的 .php 扩展名。我已经在我的配置中尝试了常见的重写。 我希望有人知道我还没有使用的答案。

您可以使用 .htaccess

在你的 PHP 文件所在的目录中创建一个新文件,创建一个名为 .htaccess 的新文件,将以下内容放入其中:

RewriteCond %{THE_REQUEST} ^.*/*\.php
RewriteRule ^(.*).php$ / [R=301,L]

在您的虚拟主机配置文件或 nginx.conf 文件中,在内部服务器块中添加以下行:

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php; }

location ~ \.php$ {
    try_files $uri =404; }

location @extensionless-php {
    rewrite ^(.*)$ .php last; }

使用前需要重启Nginx