John Davidson

.htaccess - how to replace every directory on my server with index.php


I'm trying to ensure that every directory on my server redirects users to index.php instead of letting users view my directory as below screenshot

I tried to use .htaccess to do this instead of having to create index.php on every directory but it didn't work, below is what .htaccess looks like:

ErrorDocument 404 "<H1>Page Not Found!</H1>"

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
# php -- END cPanel-generated handler, do not edit
#Alternate default index page
DirectoryIndex index.php

Can anyone help with how to solve this?

My Answer:
You can achieve this by using the following code in your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /index.php [L]

This code will check if the requested URL is a directory (-d) and if it is, it will redirect the user to index.php. Make sure to place this code in the root directory of your server.

Rate this post

4 of 5 based on 5686 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us