Error Page Redirect

If someone tries to open a non-existent file, they’ll be redirected to the error page I have set up in cPanel.

How can I set the path to be forwarded through to the domain I’m using for my error pages?

Not quite sure what you mean. Can you give an example?

It’s fairly simple. If someone visits they’ll be redirected to my error page, which is How do I set it to forward them to (Or whatever they type.)

I’m guessing it can be done with .htaccess, but I don’t know how.

I’d try this:

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^/?([a-z0-9]+)$$1 [NC,L,R=404]

I did test this, it works. See The only file that does exist is index.html.

1 Like

Just added it & I’ve left my error pages as they were. Not sure what half of this stuff is in my .htaccess file.

Line 1: Display errors (such as PHP Errors)
Line 2: Defines Character encoding (Deprecated)
Line 3: Set the timezone (You set it to Europe/London)

Lines 4-8: Error Page Redirects (from your second screenshot)

Line 9: RewriteEngine is turned on

Lines 10-12: Redirect to HTTPS

Line 13: Check for HTTPS
Line 14: Check if the requested file does not exist
Line 15: Redirect to (plus the pathname) with a 404 header.

Personally, I think you can remove Line 13. You do not need to check for HTTPS to redirect a non-existent file, especially after you have redirected everything to HTTPS.


Okay but it’s not forwarding the path. Why?

It’s not forwarding the path because ErrorDocument is defined before your custom code. You have two solutions: Remove ErrorDocument or move it to the end of the .htaccess.

1 Like

I removed the error pages in cPanel and it got rid of them from the .htaccess file. But now it redirects to an InfinityFree error. Why?

Which InfinityFree Error does it redirect to? I’m guessing 500.
Try removing line 8.

  1. Try it:

Will do.

Still won’t work.

This might be because an ErrorDocument was defined in a parent directory. Try putting your ErrorDocument at the end of the .htaccess after all of your Rewrites.

I made a working example on my site: with this .htaccess code:

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ^/?([a-z0-9]+)$$1 [NC,L]

and this is the .htaccess code in the parent dir:

php_value display_errors On
php_value mbstring.http_input auto
php_value date.timezone America/New_York

Protocols h2 http/1.1

<FilesMatch ".txt">
Require all denied

ErrorDocument 400 /http.php?n=400&t=Bad%20Request
ErrorDocument 401 /http.php?n=401&t=Unauthorized
ErrorDocument 403 /http.php?n=403&t=Forbidden
ErrorDocument 404 /http.php?n=404&t=Not%20Found
ErrorDocument 500 /http.php?n=500&t=Internal%20Server%20Error
ErrorDocument 503 /http.php?n=503&t=Service%20Unavailable

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)${REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME}.php -f  
RewriteRule ^/?([a-z0-9]+)$ $1.php [NC,L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([a-z0-9]+)$ $1.html [NC,L]

RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^/?([a-z0-9]+)$ $1.htm [NC,L]

<IfModule mod_headers.c>
Header set Strict-Transport-Security "max-age=63072000; preload"

Hope this helps your debugging.


Okay. I added the error pages back and moved them to the bottom. It’s still not working. Sorry if I’m misunderstanding.

(Not sure why the bottom box is empty. I definitely filled it in. Also, just for reference the /File404 path is just a redirect to my website homepage. I use it for tracking.)

1 Like

No, you aren’t misunderstanding. I’m confused along with you.
Right now, I can’t think of anything else to do.
There are no problems with the .htaccess code.
You could try removing ALL ErrorDocuments in / and /.htaccess and see what it does.
I am no .htaccess genie, but this should work.

1 Like

Oh no. Now I’m getting server errors when I visit the domain.

1 Like

Help!!! I’ve put it back to how it was but now it’s broken. What happened?!

Well this is brilliant.

Maybe I need to add line 8 back from before?

Try that, but it was only checking for https.