为什么我的Nginx try_files指令无法去除文件扩展名

huangapple go评论53阅读模式
英文:

Why isn't my Nginx try_files directive working to remove an extension

问题

My site is a basic PHP site with a WordPress blog installed in a subdirectory. I need to use pretty URLs, so I need to remove the .php extension from the files in root, and then get WordPress to behave like WordPress in the /blog subdirectory.

这是一个基本的PHP网站,其中安装了一个WordPress博客在一个子目录中。我需要使用漂亮的URL,所以我需要从根目录中删除文件的.php扩展名,然后让WordPress在/blog子目录中正常工作。

I've only been able to get one to work at a time and can't figure out what I'm doing wrong.

我一次只能让一个部分工作,无法弄清楚我做错了什么。

This config works for WordPress, but downloads the .php files in root. :/

这个配置适用于WordPress,但会下载根目录中的.php文件。:/

英文:

My site is a basic PHP site with a WordPress blog installed in a subdirectory. I need to use pretty URLs, so I need to remove the .php extension from the files in root, and then get WordPress to behave like WordPress in the /blog subdirectory.

I've only been able to get one to work at a time and can't figure out what I'm doing wrong.

This config works for WordPress, but downloads the .php files in root. :/

location / {
    try_files $uri $uri/ /$uri.php /blog/index.php?$query_string;
}

UPDATE

I've added a separate location so now the extension removal is working in root, but the blog links just go back to the site's home page

location / {
    try_files $uri $uri/ $uri.php;
}
    
location /blog {
    try_files $uri $uri/ /index.php?$query_string;
}

// also tried this, with the same results
location /blog {
    try_files $uri $uri/ /index.php;
}

答案1

得分: 0

I needed to add /blog/ as a path inside the /blog /location:

将`/blog/`添加为`/blog` `/location`内的路径:

location / {
try_files $uri $uri/ $uri.php;
}

location /blog {
try_files $uri $uri/ /blog/index.php;
}

英文:

Figured it out. I needed to add /blog/ as a path inside the /blog /location

location / {
    try_files $uri $uri/ $uri.php;
}
    
location /blog {
    try_files $uri $uri/ /blog/index.php;
}

huangapple
  • 本文由 发表于 2023年5月11日 01:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定