簡體   English   中英

重定向到404頁面

[英]Redirecting to a 404 page

我有一個Web應用程序,我想將用戶重定向到404頁面,而實際上他們沒有去過404頁面。 這可能嗎?

我將以下代碼行添加到了.htaccess文件中:

ErrorDocument 404 /404.php

因此,當用戶輸入(我的網站上不存在的頁面)時:

shareit.me/jsdhjfkhoe

它們被重定向到:

shareit.me/404.php

是否可以在URL保持不變的情況下重定向到404:

shareit.me/jsdhjfkhoe

如果所有路徑不存在,請使用此命令將所有路徑傳遞到404.php並保留URL:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]

在404.php文件中設置此標頭。

header("HTTP/1.0 404 Not Found");

這行:

ErrorDocument 404 /404.php

不會更改客戶端瀏覽器中的URL,因為它僅internal rewrite/404.php

我懷疑您在.htaccess中還有其他規則可以執行此完全重定向。 如果您發布完整的.htaccess文件,那么我可以進行調查。

實際上,這應該可以解決問題:

ErrorDocument 404 /404.html

->嘗試在根目錄中使用名為404.html的html元素!

如果您想查看完整的實現,請查看以下內容: https : //github.com/h5bp/html5-boilerplate/blob/master/.htaccess

否則,如果不清楚,可以看一下文檔: http : //httpd.apache.org/docs/current/mod/core.html#errordocument

如果要制作自己的自定義錯誤404頁面。 這是您需要在.htaccess上寫下的內容。

----------- .htaccess ----------------

  # 1 ---- Establish a custom 404 File not Found page ----

  ErrorDocument 404 /filenotfound.php 

  # 2 ---- Prevent directory file listing in all of your folders ----

  IndexIgnore *

----------- .htaccess ----------------

其中filenotfound.php是您自己的404定制頁面。 希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM