golang static stop index.html重定向

golang static stop index.html重定向,go,Go,因此,我有一个index.html文件,希望服务器停止显示它。的文档说明: 作为特例,返回的文件服务器重定向任何请求 以“/index.html”结尾,指向相同的路径,没有最终的 “index.html” 因此/index.html被重定向到/,/foo/bar/index.html被重定向到/foo/bar/ 为了避免这种情况,为特殊情况注册一个额外的处理程序 package main import ( "log" "net/http" ) func main() { fs :

因此,我有一个index.html文件,希望服务器停止显示它。

的文档说明:

作为特例,返回的文件服务器重定向任何请求 以“/index.html”结尾,指向相同的路径,没有最终的 “index.html”

因此
/index.html
被重定向到
/
/foo/bar/index.html
被重定向到
/foo/bar/

为了避免这种情况,为特殊情况注册一个额外的处理程序

package main

import (
  "log"
  "net/http"
)

func main() {
  fs := http.FileServer(http.Dir("."))
  http.Handle("/", fs)

  log.Println("Listening...")
  http.ListenAndServe(":3000", nil)
}

请注意,我使用insead of,因为
ServeFile
处理
/index.html
请求的方式与
FileServer
相同,没有重定向,请求目录时要呈现的默认文件是
index.html
。目录列表是找不到此文件时的回退,因此如果不删除
index.html
文件,就无法获取目录列表

如果你想要一个目录列表,你必须自己写出来,然后你可以选择什么格式和样式。基本结构非常简单,如果要直接编写,请以内部
dirList
函数为例:

http.HandleFunc("/index.html", func(w http.ResponseWriter, r *http.Request) {
    f, err := os.Open("index.html")
    if err != nil {
        // handle error
        return
    }
    http.ServeContent(w, r, "index.html", time.Now(), f)
})
w.Header().Set(“内容类型”,“文本/html;字符集=utf-8”)
格式Fprintf(w,“\n”)
对于ux,d:=范围dirs{
名称:=d.名称()
如果d.IsDir(){
名称+=“/”
}
url:=url.url{Path:name}
fmt.Fprintf(w,“\n”,url.String(),htmlReplacer.Replace(名称))
}
格式Fprintf(w,“\n”)

重定向的是什么?是否要目录列表而不是提供默认的“index.html”?@JimB Yes can,完成了吗?我希望目录列表始终显示,即使有index.htmlNo I type/并且我希望目录显示而不是index.html
我有一个index.html文件,希望服务器停止重定向到它。
不同,我希望目录显示而不是index.html
,因此请重新表述您的问题。
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for _, d := range dirs {
    name := d.Name()
    if d.IsDir() {
        name += "/"
    }
    url := url.URL{Path: name}
    fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
}
fmt.Fprintf(w, "</pre>\n")