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.