Stop apache2 from serving files and redirect to specific PHP file to handle the request I have built a similar framework to Laravel or Express for Node.js and it is working nicely, that is until I make a request to the path that exists on the server.
To prevent Apache2 from serving files and always redirect requests to your web/index.php file, you can modify your .htaccess file with the following rules:
... : Denies access to the .env file in the web directory.
With these rules in place, all requests to your server will be redirected to web/index.php, regardless of whether the requested path exists on the server as a file or directory. Additionally, directory listing will be disabled and access to the .env file will be denied.
RewriteEngine On
# Redirect all requests to index.php
RewriteCond %{REQUEST_URI} !=/web/index.php
RewriteRule ^ /web/index.php [L]
# Disable directory listing and prevent access to .env file
Options -Indexes
<Files ".env">
Order allow,deny
Deny from all
</Files>
Here's what each line of this code does:
RewriteEngine On: Enables the mod_rewrite engine for this .htaccess file.
RewriteCond %{REQUEST_URI} !=/web/index.php: Prevents infinite looping by excluding requests to /web/index.php from being redirected again.
RewriteRule ^ /web/index.php [L]: Redirects all requests to /web/index.php.
Options -Indexes: Disables directory listing for all directories in the web directory.
Comments
Post a Comment