PHP - Display a 404 Error without redirecting to another page

后端 未结 6 1556
天涯浪人
天涯浪人 2020-12-05 05:38

I want to display a 404 Error if a user reaches a page that exists but I don\'t want him/her to see.

I don\'t want to do redirect (that would cause the address bar t

6条回答
  •  孤城傲影
    2020-12-05 06:31

    In my experience the best way to show 404 error document without changing URL is to define error document in apaches .htaccess file:

    RewriteEngine on
    RewriteBase /
    
    # Defines 404 error pages content:
    ErrorDocument 404 /my_root/errors/404.html
    
    # for all invalid links (non existing files):
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* - [L,R=404]
    
    # for some valid links (existing files to be un-accessible):
    RewriteCond %{THE_REQUEST} ^.*some_file.php.*$ [NC]
    RewriteRule .* - [L,R=404]
    

提交回复
热议问题