大网数据广告活动
网站运营与故障问答

How to have .htaccess display 404.html for child pages

· 52CE 我爱测

I am working on a website example.com that has an error page example.com/404.html . Here's my entire .htaccess file (Apache): # Forces HTTPS RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQU…

问题原文

原帖链接 · heyycap · CC BY-SA 4.0

I am working on a website example.com that has an error page example.com/404.html.

Here's my entire .htaccess file (Apache):

# Forces HTTPS

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# 404 redirect

ErrorDocument 404 /404.html

# Disables directory indexing (being able to view a list of files instead of the actual webpage)

Options -Indexes

I'm using relative 404 so that it will display the content of the 404 error page without actually redirecting the user (and thereby changing the URL). This works great for upper level pages like example.com/nonexistentpage.html.

However, I am not sure how to handle child pages, like example.com/parent/nonexistentpage.html.

Currently, I have duplicated my existing 404.html page, modified it with ../ in all the URL references so it looks for resources in the correct location, and put the new "child" 404.html pages in each of my "parent" folders where there are child pages. When I access example.com/parent/404.html, the error page displays correctly with all styling and links where they should be. However, when I access example.com/parent/nonexistentpage.html, although the URL doesn't change, I get a generic, unstyled 404 error message instead of my custom 404.html page.

Am I missing something/doing something wrong? The goal is for the error page content to display when accessing nonexistent child pages while not redirecting/changing the actual URL. I also tried commenting out Options -Indexes, but that had no effect.

Thanks for any help/suggestions.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

You shouldn't have to do anything. You would normally serve the same ErrorDocument regardless of what URL caused the error. In your case, you are serving /404.html in the document root of your site.

However, it sounds as if you are using relative URLs to your static resources inside your error document. This is never going to work (unless you set a base tag - but this is not without its caveats - see reference link below) since the error document could be served from anywhere and so the browser will resolve your relative URLs incorrectly (or at least, not as you intended). Instead, you need to use root-relative URLs (starting with a slash, as you have done for /404.html) or absolute URLs (including scheme + hostname).

when I access example.com/parent/nonexistentpage.html, although the URL doesn't change, I get a generic, unstyled 404 error message

Assuming your ErrorDocument directive is still set to serve /404.html and you are using relative paths in /404.html, then any relative URL-paths will naturally resolve relative to the /parent "directory", not the document root, as it would otherwise do if requesting example.com/nonexistentpage.html.

Remember it is the browser that resolves relative URLs, based on the URL being requested. This has nothing to do with the filesystem path on your server.

See this other related question for additional examples and discussion about the use of the base tag:

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-09-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。