This .htaccess
file is configured to handle several key functionalities, including HTTPS redirection, removing .php
extensions from URLs, restricting access to .php
files directly, and specifying the PHP version to be used.
-
Redirect HTTP to HTTPS
Ensures all HTTP traffic is redirected to HTTPS for secure browsing. -
Remove
.php
Extension from URLs
Allows pages to be accessed without specifying the.php
extension, improving SEO and user-friendliness. -
Restrict Direct Access to
.php
Files
Prevents direct access to.php
files in URLs, returning a 404 error if attempted. -
Specify PHP Version
Configures the server to use a specific version of PHP (PHP 7.4
in this case).
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
- Redirects all HTTP requests to their HTTPS equivalent.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
- Maps requests without an extension to
.php
files while hiding the.php
extension.
RewriteCond %{THE_REQUEST} "\.php"
RewriteRule ^ - [L,R=404]
- Denies access to URLs explicitly containing
.php
in the request.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
- Configures the server to use PHP 7.4. Adjust this as needed for your environment.
- Place the
.htaccess
file in the root directory of your web application. - Ensure your server has
mod_rewrite
enabled. - Adjust the PHP handler if necessary, depending on your hosting environment.
- Test the configuration to ensure proper functionality.
- Server Requirements: Apache with
mod_rewrite
andmime_module
enabled. - Tested PHP Versions: PHP 7.4 (adjustable in the file).