Fastest way to add a 404 page

What is the simplist way to add a 404 page to a site?

I have tried reading up on it but seems complictaed. I dont want to spend too long on it, but a client clicked on an old link and is now unimpressed

THANKS

This would be my best option, I think.
Create a simple 404 page in your file and add a line in the htaccess file

http://forums.realmacsoftware.com/t/custom-404-will-i-crash-google/7783?u=jabostick

2 Likes

@jabostick is correct. RW is so powerful and easy to do all of this within the project itself. Build your page in RW. I name the folder 404. Add this line of code in the htaccess file:

ErrorDocument 404 http://YourWebsite.com/404/index.html

Link to my post on building a .htaccess file inside RW

Example 404 page.

1 Like

Im not quite getting it!

My page is published and can be found http://www.mysite.com/htaccess.html

I went in and added that line of code (in transmit) … but I do not get re directed

Think Im being a bit thick!

It’s simple. Here is how I would do this.

  1. In RW create the 404 page same way you create all other pages. Give it a name like “Error” or “404”, or something similar and the filename index.php. Folder name 404 or something. This page will publish/export with the rest of your RW project.
  2. In any plain-text editor (for example: TextEdit) create new htaccess.txt file. Copy all code from existing file and add new code for 404 redirecting. Do not change the name to “.htaccess” before you upload it to the server. Otherwise, it will become invisible.
  3. Backup and delete existing .htaccess file from your site on server.
  4. Upload your newly created file to the root of your public_html folder on the server (if that’s the folder where your domain’s files reside).
  5. Change the name of the file on server to “.htaccess” (add period at the beginning and delete extension .txt). You are ready now.

Here is the piece of code you should add to your htaccess.txt file (of course, use your site’s real http or https prefix and domain name). Make sure that the filename and folder name are the same as in your RW project-page.

# Access to Page Not Found
ErrorDocument 404 https://figory.com/404/index.php

Of course, the methods given by Jason and Eric are also correct. I just attempted to present my personal approach, in as simple way as possible. :slight_smile:

4 Likes

Is your domain mysite.com? If not, send a link to your site please.

I should also add that you want to create the htaccess page as a blank theme from Joe Workman.

I was just looking at this exact issue someplace else.

This comes up quite often for Rapidweavers and using the normally used htaccess code:

ErrorDocument 404 /404.html

Does not work correctly for a 404 page you create inside of RW. It only works if the user links to a non-existent page in the root level of your domain. If they link to a non-existent page at any lower level, then all the links that are relative to the 404 page (inside the 404 page) will break. And there are many crucial relative links in the page.

The way around this is a hack but to date the only known way to get around this.
In your htaccess file add:

Redirect 301 /non_existant.html /404.html
ErrorDocument 404 /non_existant.html

So what is this doing, and what do you need to make this work.

Line 2 is the normal way to link to a 404 page, but in our case, we simply say, if a 404 occurs link to a page called non_existant.html in the root of our website.
That page really really does not exist (do not create it).

Because…

In Line 1 of our code, we have said if anyone (or anything) links to /non_existant.html permanently redirect them (or it) to /404.html

A redirect will always resolve relative links in the page, so the 404.html will display correctly no matter at what level of the domain it was triggered.

So all we need to add to the RW project is a page, displaying what ever you’d like the user to see for the 404 page, and name the file 404.html and set the folder to / (the domain’s root).

2 Likes

This works for me:
1.) This goes in your htaccess file, it’s the first line of my file: “ErrorDocument 404 /404.php”
2.) Build your page in RW but name it “404.php” and put it at root level of your site, not in a folder. Best perhaps to export locally and upload with ftp so you can know for sure it’s at root level.

General tip: Avoid 404 errors due to broken links by using Integrity app often.

But does it still work if you trigger a 404 deeper in the folder structure? For example:

https://yoursite.com/subfolder/deeperfolder/non-existantpage.html

Normally if you trigger a 404 in a subfolder deeper than the root, then any relative links in the 404.html will break, and there are many when generated with RW.

Here’s one reference to the issue: https://stackoverflow.com/questions/26681655/apache-custom-404-page-relative-links-break-in-sub-directories

Yes…

It does work in “deeper” folders. For example:
This link is a good folder but bad page:
https://www.wayoflife.org/reports/badpage.php

This link is a bad folder and bad page:
https://www.wayoflife.org/badreports/test.php

It’s not working as expected…
In your example https://www.wayoflife.org/reports/badpage.php
Once you trigger the 404 then all the links in your menu are broken.
Once on the 404 page try selecting Home > Offering for example.

Also once in that 404 page, look at all the broken links to crucial files to make the page display and work correctly in the console:

If you test:
https://www.wayoflife.org/about/makeanofferingx.html
Mis-spelt “offering”
Then you will see an even worse result.

ignore… investigating more…

ignore… investigating more…

http://oneelevenwebdesign.com/imlost goes directly to the 404 page.

http://oneelevenwebdesign.com/search-engine-optimization/seo-submission/imlost goes to the 404 page.

I’m using an absolute link in the htaccess file: ErrorDocument 404 http://oneelevenwebdesign.com/404/index.html

It seems to be working. I’m writing with a little hesitation as I’m sure @Doobox and any one else on this thread with much more experience coding will for sure school me. :wink: In all seriousness, I do appreciate all the knowledge I’ve gained from this group.

In every test, I have done and every attempt I have seen made by others to implement a 404 with the usual htaccess code, this bad url should fail to resolve the links in the 404 page when triggered.

http://oneelevenwebdesign.com/search-engine-optimization/seo-submission/missing.html

Reason:

It’s more than 1 directory deep from the root.
The relative links in the 404 page do not change.
When a 404 is triggered, relative links are read as if navigating from the location that triggered the error.
This makes them incorrect from any location deeper than the root.

So, I’m at a loss why your example does not fail using the bad url above. Perhaps something else in your htaccess file is playing an inadvertent role here.

This is why in all Stack overflow questions relating to this issue, the recommended answer is to make all the links in your 404 page absolute, then there is no issue as to where it is triggered from.
The problem with this when using RW, is you can’t do that without editing the html after uploading to the server (impractical).

It’s just occurred to me that you will probably gain one extra directory level without seeing the failure because you have the 404 in a subfolder, the relative links are already looking backwards 1 directory.

So for you in this scenario, you would see a failure if triggered from any directory deeper than 2 as opposed to the usual 1.

IDK. It could likely be the absolute link that is making this work. Building the 404 and htaccess files in RW makes it easy to replicate for every project and see all your files in one application. The only impractical thing is logging into the C-Panel to change the name to .htaccess then coming back to RW to select the htaccess page to draft so it doesn’t upload it again if you’re worried about anyone having your htaccess information.

Below is my htaccess that I use for all my projects.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]

#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.oneelevenwebdesign.com [NC]
RewriteRule ^(.*)$ http://oneelevenwebdesign.com/$1 [L,R=301]

ErrorDocument 404 http://oneelevenwebdesign.com/404/index.html

No that makes no difference.

Digital Marketing Agency | One Eleven Web Design | Stockton, CA and
Digital Marketing Agency | One Eleven Web Design | Stockton, CA sends you to the 404 page.

Or it could be this actually doing exactly what I suggest as the hack, by redirecting to the 404

1 Like