user2320500
user2320500

Reputation: 169

Redirecting to a 404 page

I have a web app, and I want to redirect users to the 404 page without them actually ever going to a 404 page. Is this possible?

I added this line of code to my .htaccess file:

ErrorDocument 404 /404.php

So when a user types in (a page that does not exist on my website):

shareit.me/jsdhjfkhoe

They are redirected to:

shareit.me/404.php

Is there a way to redirect to the 404 while the URL remains:

shareit.me/jsdhjfkhoe

Upvotes: 0

Views: 101

Answers (4)

user3054322
user3054322

Reputation: 1

If you want to make your own customized Error 404 page. Here's what you need to write down on your .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 ----------------

Where filenotfound.php is your own 404 customized page. Hope it helped.

Upvotes: 0

transilvlad
transilvlad

Reputation: 14532

Use this to pass all paths to 404.php if they do not exist and preserve the URL:

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

Set this header in the 404.php file.

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

Upvotes: 1

Severin
Severin

Reputation: 8588

Actually this should do the trick:

ErrorDocument 404 /404.html

-> Try using an html element named 404.html in your root!

Have a look at this if you want to see a full implementation: https://github.com/h5bp/html5-boilerplate/blob/master/.htaccess

Otherwise you could have a look at the documentation if anything is unclear: http://httpd.apache.org/docs/current/mod/core.html#errordocument

Upvotes: 0

anubhava
anubhava

Reputation: 786261

This line:

ErrorDocument 404 /404.php

doesn't change the URL in the client browser since it does only internal rewrite to /404.php

I suspect you have some other rule in your .htaccess doing this full redirect. If you post your full .htaccess then I can investigate.

Upvotes: 0

Related Questions