【问题标题】:Disable .php extension but run files without .php as php file禁用 .php 扩展名但将没有 .php 的文件作为 php 文件运行
【发布时间】:2017-02-05 19:51:25
【问题描述】:

我在使用 FastCGI 启用的 NGINX 和 PHP7 中遇到 URL 重写问题。

要求如下。

原始网址:http://example.com/somename1.php
响应:会抛出404错误

原网址:http://example.com/somename1
响应:会执行http://example.com/somename1.php

原始网址:http://example.comhttp://example.com/
响应:会执行http://example.com/index.php

我浏览了以下网址,但它们不能满足上述需求。

  1. How to remove both .php and .html extensions from url using NGINX?
  2. remove .php from url with rewrite rule
  3. remove .php extension from url in nginx

下面给出了示例“默认”文件的内容。

server {  
    listen 80 default_server;  
    listen [::]:80 default_server;  
    root /var/www/html;  
    index index.php index.html index.htm index.nginx-debian.html;  
    server_name _;  
    location / {  
        try_files $uri $uri/ =404;  
    }  
    location ~ \.php$ {  
        include snippets/fastcgi-php.conf;  
    }  
    location ~ /\.ht {  
        deny all;  
    }  
}  

任何人都可以帮助我解决这个问题或指向我可以获取更多信息的其他页面吗?

谢谢

【问题讨论】:

    标签: url nginx php-7


    【解决方案1】:

    你有三个要求:

    原网址:http://example.com/somename1.php 回复:会抛出 404错误

    使用internal 指令。详情请见this document

    例如:

    location ~ \.php$ {
        internal;
        ...
    }
    

    原网址:http://example.com/somename1 回复:会执行 http://example.com/somename1.php

    有多种方法可以实现无扩展的 PHP。您问题中的第一个链接使用try_files、一个命名位置和一个rewrite

    原始网址:http://example.comhttp://example.com/ 回复:它 将执行http://example.com/index.php

    根据您的示例,在这种情况下,通常会使用 index 指令。如果无扩展的 PHP 方案搞砸了,请使用完全匹配的位置块:

    location = / {
        rewrite ^ /index.php last;
    }
    

    请参阅this document 了解更多信息。

    【讨论】:

    • 此答案不完整,对遇到相同问题的人没有帮助。与其简单地依赖将来可能无法使用的链接,不如在此处显示所有部分的完整答案。换句话说,服务器块在工作时是什么样子的?
    猜你喜欢
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    相关资源
    最近更新 更多