【发布时间】:2017-05-27 21:08:03
【问题描述】:
我已经阅读了十几个复习题。大多数问题都是针对 .php 的,但我有简单的 .html 文件,所以 about => about.html 。但我无法让 nginx 做到这一点:
- 接受带有或不带有“/”的网址
- 将 domain.com/about 和 domain.com/about/ 重定向到 domain.com/about.html
- 在 URL 中添加斜杠,这样如果我写 domain.com/about 它就会写 domain.com/about/
我已尝试使用此代码
rewrite ^/([a-zA-Z0-9]+)$ /$1.html;
但它在以“/”结尾的 url 上给了我 404 错误。也试过这个
rewrite ^([a-zA-Z0-9]+)/?$ $1.html 有同样的错误。
有人建议我应该使用 try try_files?
我也试过这个,它给出了 500 错误:
location / {
rewrite ^(.*)$ /%1 redirect;
rewrite ^/([^\.]+)$ /$1.html break;
}
我尝试将 apache 从建议 here 转换为 nginx (winginx.com),但没有成功。
此代码不起作用。我正在尝试重定向并添加斜杠:
if (!-e $request_filename){
rewrite ^/(.*)/$ /$1 redirect;
}
if (!-e $request_filename){
rewrite ^/(.*[^/])$ /$1/ redirect;
}
任何建议都会让我高兴。
【问题讨论】:
-
最简单的解决方案是将
about.html重命名为about/index.html然后nginx的索引模块几乎可以满足您的需求。 -
这听起来有点矫枉过正。我必须为每个 html 文件创建一个文件夹,而不是单个 html 文件,并将 index.html 文件中的内容放入该文件夹中?!?
标签: .htaccess nginx url-rewriting