I set up a vps, and try to redirect error 404
for example when someone try to random filename on my site like login.php
my server show: File not found.
i try to create .htaccess
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 /index.php
but it did’t work.
Do you have an idea, how to edit this page, and redirect it index.php instead of show file not found.
Block access to specific files and customize 404 page settings
To enhance website security, you may want to block users from accessing specific files (such as’ login. php ') and set up custom 404 error pages. Here is a brief guide on how to achieve these goals on the Apache server.
Block access to specific files
Use the . htaccess file * *:
-Create or edit the ‘. htaccess’ file in the directory containing’ login. php ', and add the following code to disable access:
<Files "login.php">
Require all denied
</Files>
Move files out of the web root directory * *:
-Place sensitive files outside the root directory of the web server, such as moving them from ‘/var/www/html’ to ‘/var/www/private’.
Server Configuration * *:
-If you have server configuration permissions, you can add rules similar to ‘. htaccess’ in’ httpd.conf ‘or’ apache2.conf '.
Other methods * *:
-Rename or delete files that are no longer needed.
-Use the routing function of the PHP framework to control access.
Set custom 404 page
Edit the . htaccess file * *:
-Ensure that the ‘. htaccess’ file contains:
ErrorDocument 404 /index.php
-This will redirect users to ‘index. php’ when accessing non-existent files.
Check Apache configuration * *:
-Ensure that the ‘AllowOverride’ directive includes’ FileInfo ‘or is set to’ All ‘to allow custom settings in’. htaccess’ to take effect.
Test Configuration * *:
-Attempt to access a non-existent file, such as’ http://yourdomain/nonexistentfile.php ‘, and verify if it redirects to’ index. php '.
Other precautions * *:
-Ensure that the ‘index. php’ file permissions are correct (usually 644).
-Restart the Apache service after modifying the configuration.
-Check server logs to diagnose issues.
Through these steps, you can effectively prevent users from accessing specific files and set up custom 404 pages to improve website security and user experience.