如何使用 NGINX 从 url 中删除 .php 和 .html 扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21911297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:32:19  来源:igfitidea点击:

How to remove both .php and .html extensions from url using NGINX?

phphtmlnginx

提问by anthropophagus

I want my nginx make display all url's clean.

我希望我的 nginx make 显示所有 url 的干净。

With some research I've made the first case to work. It`s done by following configuration:

通过一些研究,我使第一个案例奏效。它通过以下配置完成:

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

It works for indexhtml.html displaying as indexhtml, but nothing happens with .php. If I change $uri.html to $uri.php, it works neither for .html, neither .php. I`ve tried to put something similar in php location but without any success.

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

Any advices?

有什么建议吗?

回答by Hyman

From what I've researched, if you append your /etc/nginx/conf.d/domain.tld.conf file to include:

根据我的研究,如果您附加 /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 ^(.*)$ .php last;
}

Then restart nginx and give it a go. Hopefully this will help you! More information can be found (where I found it) here @ tweaktalk.net

然后重新启动nginx并试一试。希望这会帮助你!更多信息可以在这里找到(我找到的地方)@tweaktalk.net

回答by Mohammad AbuShady

No need for extra blocks and named locations and everything. Also move the indexline outside the location block

不需要额外的块和命名的位置和一切。同时将index线移到位置块之外

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

Keep in mind that if you have a folder and a file with the same name inside the same folder, like /folder/xyz/and /folder/xyz.phpyou won't be able to run the php file if the folder xyzcontains an index.phpor index.html, just keep this in mind.

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

回答by cnst

To further Mohammad's answer, you might also want to offer redirects from .htmland .phpto the extensionless versions.

为了进一步穆罕默德的答案,你可能还需要从提供重定向.html.php对扩展名的版本。

This can be accomplished due to the fact that $request_uricontains "full original request URI (with arguments)", and is not affected by the internal rewrites that are not visible to the user.

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

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

回答by ?ukasz Pniewski

Perhaps this may be of use for you... It' Simple and gets the job done:

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

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