How to display Apache's default 404 page in PHP

前端 未结 3 855
你的背包
你的背包 2021-02-19 03:55

I have a webapp that needs to process the URI to find if a page exists in a database. I have no problem directing the URI to the app with .htaccess:

Options +Fo         


        
3条回答
  •  深忆病人
    2021-02-19 04:32

    The only possible way I am aware of for the above scenario is to have this type of php code in your index.php:

    And then slightly modify your .htaccess like this:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{QUERY_STRING} !notFound=1 [NC]
    RewriteRule ^(.*)$ index.php?p=$1 [NC,L,QSA]
    

    That way Apache will show default 404 page for this special case because of extra query parameter ?notFound=1 added from php code and with the negative check for the same in .htaccess page it will not be forwarded to index.php next time.

    PS: A URI like /foo, if not found in database will become /foo?notFound=1 in the browser.

提交回复
热议问题