To remove .html or .php extensions from the URLs of a website, you typically need to configure your web server to handle URLs without these extensions properly. This process may vary depending on the web server you are using, but here are general steps for common setups:
Apache Server:
If you are using Apache, you can achieve this using the mod_rewrite module. You need to create or modify the .htaccess file in your website’s root directory. Here’s an example of how you can remove .html extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
This set of directives tells Apache to internally rewrite requests for URLs without the .html extension to the corresponding HTML file. You can do something similar for PHP files if needed.
Nginx:
If you are using Nginx, you can achieve similar results using try_files directive within your server block. Here’s an example:
server {
…
location / {
try_files $uri $uri.html $uri/ =404;
}
…
}
This configuration tells Nginx to try serving the requested URL first as-is, then with a .html extension, and then as a directory. If none of these are found, it will return a 404 error.
Other considerations:
Ensure that your links within your website do not include the file extensions. Use relative URLs or configure your website to generate clean URLs. Update any internal links and references to the pages without the file extensions. Ensure that your web application (if any) does not rely on file extensions for routing or linking.
How To Remove .Html or .Php From The URL or link Of A Website?
For HTML Websites
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L,NE]
For Php Websites
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]
Read Also:- Mastering Subdomain and Addon Domain Management in cPanel: A Step-by-Step Guide