如何使用NGINX从url中移除.php和.html扩展名? [英] How to remove both .php and .html extensions from url using NGINX?

查看:2009
本文介绍了如何使用NGINX从url中移除.php和.html扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的nginx能够显示所有网址的干净。



通过一些研究,我创造了第一个案例。这是通过以下配置完成的:

  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位置放置类似的东西,但没有任何成功。



任何建议?

解决方案

从我研究过的内容来看,如果你追加你的/etc/nginx/conf.d/domain.tld.conf文件来包含:

 位置/ {
try_files $ uri $ uri.html $ uri / @ extensionless-php;
index index.html index.htm index.php;
}

位置〜\.php $ {
try_files $ uri = 404;
}

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

然后重新启动nginx,然后继续。希望这会帮助你!更多的信息可以找到(我发现它的地方)这里@ tweaktalk.net < a>


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

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.

Any advices?

解决方案

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

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

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从url中移除.php和.html扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆