Skip to the content.

Remove .php From URL and Make User Friendly URL In PHP

To remove the .php extension from URLs in a Core PHP website, you’ll need to utilize URL rewriting techniques. This can be accomplished through Apache’s mod_rewrite module, allowing for the creation of user-friendly URLs.

Steps:

  1. Enable mod_rewrite: Ensure that the mod_rewrite module is enabled in your Apache server configuration. Open your httpd.conf file and verify that the following line is not commented out: LoadModule rewrite_module modules/mod_rewrite.so

If you make any changes, remember to restart your Apache server.

  1. Create a .htaccess file: In the root directory of your website, create a file named .htaccess if it doesn’t already exist.

  2. Add Rewrite Rules: Open the .htaccess file and add the following lines:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Remember that this approach assumes you’re using an Apache web server. If you’re using a different server, the steps may be slightly different. Always back up your files before making changes to configuration files.

Ref: pritam maheshwari - Medium