Last updated on August 29th, 2024 at 04:10 am
Millions of websites run on PHP platform. Most blogs use WordPress which also uses PHP. When you are running a PHP-based website, then by default, your web server such as Apache server adds .php to URL for PHP files. This is not user-friendly since they need to remember to add .php extension to all requests. If they omit it, then they may get 404:Page Not Found error message. So it is recommended to remove URL extensions from your website. Here’s how to remove .php from URL in Apache. You can use these steps to remove .php from URL in WordPress, Magento, Drupal, Joomla and other CMS systems. You can also use it to remove index.php from URLs.
Why Remove .php extension from URL
Typically, every PHP website’s URLs contain .php extension. Your web server treats the URL with and without .php extension differently. By default, it is configured to serve requests with .php URL extension. If users do not include this extension in their requests, then your web server will return ‘Page Not Found’ error. Also, it can be tedious for users to remember URL extensions for all URLs. Also, if any malicious user or bot visits your site, they can easily guess your site technology just by looking at the URL extension, and then exploit its vulnerabilities. Therefore, it is essential to remove .php extension from URL as soon as you launch your site or blog, if not later.
Remove .php extension from URL
Before proceeding further, you need to enable mod_rewrite in Apache web server. Here are the steps to enable mod_rewrite (.htaccess) in Apache web server.
1. Open htaccess file
Open terminal and run the following command to open .htaccess file. Please change the file path to .htaccess file as per your requirement.
$ sudo vi /var/www/html/.htaccess
Bonus Read : How to Remove Trailing Slash from URL
2. Remove .php extension from URL
Add the following lines to redirect all URLs with .php extension to URLs without file extension.
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php [NC,L]
In the above code, the first line checks that the request is a URL and not a directory. The second line redirect matching URLs with .php extension to those without file extension. The last line redirects matching requests to URLs without php file extension.
Similarly, you may also add the following code to remove .html extension from URL.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
These are similar to removing index.php from Apache/PHP websites.
Bonus Read : How to Disable mod_security in Apache
3. Restart Apache web server
Restart Apache server with following command
$ sudo systemctl restart httpd
4. Verify Changes
Open web browser and request any URL from your website, with .php extension. You should be redirected to the page without this extension.
Conclusion
In this article, we have learnt how to remove .php URL extension in Apache/Wordpress sites. You can use the same method for all PHP based sites, that run on Apache server. If you do not redirect URLs with .php extension to those without, then your server will see these two URLs as separate. In such cases, it will return ‘Page Not Found’ error when you do not include .php extension. Please note, the approach remains the same even if you are using another web server – you need to redirect all URLs with extension, to those without extension.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.