简体   繁体   中英

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

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");

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM