簡體   English   中英

刪除所有頁面上的 .php 擴展名 (NGINX)

[英]Remove the .php extension on all pages (NGINX)

在 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;
     }

這可能就是你要找的

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM