By default, Apache adds .php to URL for PHP files. 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.
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]
Bonus Read : How to Disable mod_security in Apache
3. Restart Apache web server
Restart Apache server with following command
$ sudo systemctl restart httpd
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!