我正在編寫一個(gè) web 服務(wù)器,我有 Not Found Handler 函數(shù)。登錄、注冊、查看頁面等所有處理功能均正常工作。我也有這樣的一行:router.NotFoundHandler = http.HandlerFunc(Handlers.NotFoundHandler) Handlers 是我的包,包括下一個(gè)代碼:func NotFoundHandler(w http.ResponseWriter, r *http.Request) { //title has to be 404.html title := config.Page404 title = config.WebAppex + title fmt.Println("title: " + title) /*HERE IS QUESTION LINE*/ r = http.NewRequest("GET", config.Page404, nil) fmt.Println(r.URL.Path) logger.Info("Sending... ", title) //load page p, err := loadPage(title) if err != nil { logger.Error("ERROR : ", err.Error()) p, _ := loadPage(config.Page404) fmt.Fprintf(w, "%s", p.body) //wired way to send HTML page return //just return because of printing dry p.body } //serve it http.ServeFile(w, r, p.dir) logger.Info(p.dir, " has been sent")}所以問題是:如果我嘗試訪問 localhost:8080/nothing,那么我得到了很好的 Page404 但是如果我訪問 localhost:8080/nothing/nothing 或 localhost:8080/nothing/nothing/nothing 等等,我會感到枯燥404.html 沒有任何 CSS。所以我認(rèn)為問題是請求,所以我用“/404.html”(它是config.Page404)的方式創(chuàng)建了新的,但沒有任何改變。我讀過關(guān)于 gorilla mux Subrouter 的文章,它可能有助于僅排除 localhost:8080/nothing,但我需要所有情況。那么有沒有辦法排除所有不存在的頁面?UPD:我的 loadPage 功能:func loadPage(title string) (*page, error) { logger.Info("Trying to load", title) //writing from title to body body, err := ioutil.ReadFile(title) if err != nil { return nil, err } return &page{dir: config.HOMEDIR + title, body: body}, nil}type page struct { dir string body []byte}謝謝!
1 回答

慕運(yùn)維8079593
TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊
您可能正在使用相對的 CSS 樣式表路徑為 404 頁面提供服務(wù),當(dāng)從/nothing/nothing/nothing
.
要么使用該<base>
標(biāo)簽在任何地方獲取絕對鏈接,要么將您的請求重定向到想要的頁面。
- 1 回答
- 0 關(guān)注
- 201 瀏覽
添加回答
舉報(bào)
0/150
提交
取消