【问题标题】:How to remove both .php and .html extensions from url using NGINX?如何使用 NGINX 从 url 中删除 .php 和 .html 扩展名?
【发布时间】:2014-03-21 14:27:03
【问题描述】:

我希望我的 nginx 使显示所有 url 的内容都干净。

通过一些研究,我制作了第一个案例。通过以下配置完成:

location / {
    root   html;
    index  index.html index.htm index.php;
    try_files $uri.html $uri/ =404; 
}

它适用于 indexhtml.html 显示为 indexhtml,但 .php 没有任何反应。如果我将 $uri.html 更改为 $uri.php,它既不适用于 .html,也不适用于 .php。我试图在 php 位置放置类似的东西,但没有任何成功。

有什么建议吗?

【问题讨论】:

  • 等一下,您要添加不带点的扩展名吗?
  • 不,我根本不需要扩展。那s only for example to understand, that Im 不是怪人,谁s having both index.php and index.html files on my server. However, its 已经解决了。

标签: php html nginx


【解决方案1】:

根据我的研究,如果您将 /etc/nginx/conf.d/domain.tld.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 ^(.*)$ $1.php last;
}

然后重启 nginx 试试看。希望这会对您有所帮助!可以找到更多信息(我在哪里找到的)here @ tweaktalk.net

【讨论】:

  • 有趣。如果我从我之前的配置中删除codetry_files $uri.html $uri/ =404;code 并添加你的,那么一切都适用于php,但不适用于html。如果我不删除前一行并添加您的,则会显示“问题加载页面”。我怎样才能将它们结合起来同时工作?
  • 尝试将 'location' 下的 'try_files' 行更改为: try_files $uri $uri.html $uri/ @extensionless-php;或者可能:try_files $uri $uri.html $uri/ @extensionless-php $uri/ =404;
  • 非常感谢!我替换了 try_files $uri $uri/ @extensionless-php;使用 try_files $uri.html $uri/ @extensionless-php;它最终适用于 html 和 php。太好了!
  • 如果你在 Windows 上安装了 nginx,n 似乎找不到“/etc/nginx/conf.d/domain.tld”,那么这实际上与“nginx/conf/nginx.conf”相同"
  • 将此用于您的 ddns 服务器。
【解决方案2】:

不需要额外的块和命名的位置和一切。同时移动indexoutside the location block

server {
  index index.html index.php;
  location / {
    try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
  }
  location ~ \.php$ {
    try_files $uri =404;
    # add fastcgi_pass line here, depending if you use socket or port
  }
}

请记住,如果您在同一文件夹中有一个文件夹和一个同名文件,例如 /folder/xyz//folder/xyz.php,如果文件夹 xyz 包含您将无法运行 php 文件index.phpindex.html,请记住这一点。

【讨论】:

  • @elbowlobstercowstand,只是另一个补充,当使用$query_string时,我更喜欢使用$is_args而不是普通的?,因为没有尾随而没有实际的参数时会更干净一点网址中的?
  • 只是补充一下,使这项工作的重要一点是 =404 需要在 .php 块的 try_files 中,而不是在 location / one 的末尾,否则你会得到.php 文件 确实 存在的 URL 的 404 错误。
  • @elbowlobstercowstand 是对的,我回滚了他上次的编辑,如果没有添加 ?$query_string,nginx 将看不到 _GET 数组。
【解决方案3】:

为了进一步了解Mohammad's answer,您可能还想提供从.html.php 到无扩展版本的重定向。

这可以实现,因为$request_uri 包含“完整的原始请求 URI(带参数)”,并且不受用户不可见的内部重写的影响。

server {
  index index.html index.php;
  location / {
    if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
    try_files $uri $uri/ $uri.html $uri.php?$args;
  }
  location ~ \.php$ {
    if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1?$args;  }
    try_files $uri =404;
    # add fastcgi_pass line here, depending if you use socket or port
  }
}

【讨论】:

  • "指令如果在位置上下文中使用时出现问题,在某些情况下它并没有达到您的预期,而是完全不同。在某些情况下它甚至会出现段错误。通常最好避免如果可能的话。” nginx.com/resources/wiki/start/topics/depth/ifisevil
  • @Tag,听起来应该有一个单独的页面ifisevilisevil;如果您实际上阅读了您链接的ifisevil 文档,您会注意到从if 中执行return 被明确宣传为始终是100% 安全的,而这正是我在回答中所做的
  • 这可能是安全的,但不是最佳做法。请阅读Pitfalls and Common Mistakes
  • @Tag,因为什么时候使用安全的if 不是最佳做法?另外,您为什么要链接到一个甚至没有声称它不是最佳做法的页面,却表现得好像它确实声称它不是?
【解决方案4】:

也许这可能对您有用...它很简单,可以完成工作:

location / {
  rewrite ^/([^\.]+)$ /$1.html break;
}

【讨论】:

  • 它工作了,但现在没有处理 php...php 已下载!
【解决方案5】:

这已经为我工作了 5 年多。

location / {
               
        try_files $uri/ $uri.html $uri.php$is_args$query_string;

}

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 2017-02-27
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多