简体   繁体   中英

php how to redirect when page not found or access denied, or no permission

How can I make my website to redirect to a particular page when user attempts to folder or files that are not allowed or when they access not existing web page? I think it is done using some kind of server side scripts, please help me to find it.

If you want to prevent users from certain content use 403 Forbidden header . Not 404 . You might want to use 404 to fool the user, but later when something goes wrong it'll be very difficult do debug. Also this is not standard practice.

You can throw a 403 Forbidden error using htaccess.

RewriteEngine On
RewriteBase /
# Protect application and system files from being viewed
RewriteRule ^(secret_dir1|secret_dir2|secret/file.php) - [F, L]

On PHP end you should invoke a header if some one is trying to get an unauthorized access.

header("HTTP/1.1 403 Forbidden");

that should be done in .htaccess file, add this:

ErrorDocument 404 index.php // it could be any file/page

Check this link out: http://www.awesomephp.nl/htaccess/custom-error-pages

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