【问题标题】:Remove the .php extension on all pages (NGINX)删除所有页面上的 .php 扩展名 (NGINX)
【发布时间】:2021-02-01 07:56:14
【问题描述】:

尝试了 Stack 上的所有解决方案后,我无法删除 .php 扩展名。

我设法使 URL 可访问:www.mydomain.com/login 但用户始终可以将 URL 更改为 www.mydomain.com/login.php,我想避免这种情况。

这是我的配置的一部分:

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

    location ~ \.php$ {
         try_files $uri =404;
         include fastcgi_params;
         fastcgi_intercept_errors on;
         fastcgi_pass unix:/run/php/php7.3-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
     }

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

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
     }

【问题讨论】:

  • 也许您可以使用路由器而不是使用网络服务器来提供文件?
  • 我不喜欢用路由器
  • 要从原始请求中删除.php,请使用:if ($request_uri ~ ^(.*)\.php(\?.*)?$ { return 301 $1$2; }
  • 勾选this一个。

标签: php nginx url-rewriting


【解决方案1】:

这可能就是你要找的东西

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

对于.html 扩展,您只需添加此

RewriteRule ^([^\.]+)$ $1.html [NC,L]

如果你想在末尾添加一个斜杠,你可以忽略上面的代码并插入下面的代码。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

如果您使用的是GoDaddy,您需要在.htaccess 文件的顶部启用MultiViews

Options +MultiViews

【讨论】:

  • 你为什么用 apache 解决方案提出一个关于 nginx 的问题?这不是 OP 想要的。
  • 开玩笑,我什至没有意识到这一点
猜你喜欢
  • 1970-01-01
  • 2016-11-03
  • 2021-12-08
  • 1970-01-01
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多