- 1 year ago
insert extension into files without extension by htaccess
To insert an extension into files that don't have an extension using an .htaccess
file, you can use mod_rewrite rules in Apache. Here's an example of how you can achieve this:
Create or edit the
.htaccess
file in the root directory of your website.Add the following mod_rewrite rules to the
.htaccess
file:apacheRewriteEngine On
# Rewrite files without extension to include .html extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])$ /$1.html [L]
This rule checks if the requested filename doesn't exist as a file or directory. If it doesn't, it appends the
.html
extension to the URL.You can modify the
.html
extension to any other extension you prefer (e.g.,.php
,.htm
, etc.).Save the
.htaccess
file.
After applying these changes, when a user requests a file without an extension (e.g., example
), the server will internally rewrite the URL to include the specified extension (e.g., example.html
) and serve the corresponding file.
Please note that mod_rewrite must be enabled on your Apache server for these rules to work. Additionally, ensure that you have the necessary permissions to modify the .htaccess
file.
It's recommended to test the changes in a development or staging environment before applying them to a production website.