【问题标题】:Nginx extensionless PHP rewrite not workingNginx 无扩展 PHP 重写不起作用
【发布时间】:2015-07-08 03:33:46
【问题描述】:

我一直在尝试重写我的php 页面,以便它们在浏览器中显示时没有扩展名。例如,我服务器上www.example.com 的链接会加载一个名为about-us.php 的页面,而在浏览器中,用户会看到www.example.com/about-us

我一直在尝试按照这些教程来尝试使其正常工作,但遗憾的是它们不起作用。

Tweak Talk Link

Stack Overflow Link

这是我的 nginx sites-available/default 文件内容。

# Server Block
server {
    # Listening Ports
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    # Root Location Initialization
    root /home/zach/Documents/Workspaces/www;
    index index.php index.html index.htm;

    # Server Name
    server_name localhost;

    # Root Location
    location / {
        try_files $uri $uri/ @extensionless-php;
    }

    # Error pages
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /home/zach/Documents/Workspaces/www;
    }

    # PHP Handling
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

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

我所有指向php 页面的链接仍然在浏览器中显示.php 扩展名,有人可以解决我的问题吗?

【问题讨论】:

  • 这个问题可能更适合Server Fault
  • @AlphaDelta 这个问题是关于 nginx - 它直接涉及主要用于编程的工具
  • 肯定是服务器故障。
  • @scrowler 确切地说这是一个关于 nginx 的问题,一个 服务器软件,它不是编程或脚本问题。

标签: php nginx url-rewriting


【解决方案1】:

将以下行添加到您认为它会形成循环的确切位置! :-)

if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1?$args;  }

更多信息在这里:nginx remove .php and .html file extension

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    相关资源
    最近更新 更多