When it comes to creating a website, one of the most important things to consider is the user experience. This includes not only the design and functionality of the website but also the structure of the URLs.
A clean and simple URL structure can make it easier for users to understand the organization of the website and improve the overall navigation. Additionally, it can also help with search engine optimization (SEO) by making it easier for search engines to crawl and index your pages.
The benefits of hiding file extensions:
- Back-end technology is hidden from end users. But it’s still not hard to identify the technology for experts.
- The best pros of this are that we can easily change backend technology without affecting the SEO of the pages.
- Read more about .htaccess here
One way to improve the structure of your URLs is by removing the file extensions, such as .php and .html. This can make your URLs appear cleaner and more professional, and can also help to hide the technology used to build the website.
The process of removing file extensions from URLs can be done using the .htaccess file. The .htaccess file is a configuration file used by the Apache web server and can be used to make changes to the server’s behavior, including URL rewriting.
Removing .php
Extension from URL
For example, You need to change the URL from http://example.com/about.php to http://example.com/about.
Create a new `.htaccess` file or edit an existing one. This file should be placed in the root directory of your website. Add the following code to the `.htaccess` file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
Now, If the user accessed /about in the browser, it will show the content from /about.php. But still, if any user typed the completed URL as http://example.com/about.php, this will not redirect. Now you need to add some more rules to the `.htaccess` file.
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
This code tells the server to turn on the rewrite engine and then checks if the requested URL is not a directory and if the file with a `.php` extension exists. If both conditions are met, it rewrites the URL by removing the .php extension.
Additionally, you should also keep a backup of your original `.htaccess` file, in case you need to revert the changes.