Having .php at the end of your website URLs can make them look outdated and less user-friendly. In this tutorial, you’ll learn how to remove PHP extension from URL using simple configurations in Apache and Nginx. Cleaner URLs not only improve SEO but also enhance your site’s professional appearance and user experience.
If you’re using PHP without a framework and hosting your application on Nginx/Apache server , you might notice that your URLs look like this:
https://ongsho.com/contact.php
But wouldn’t it be cleaner and more professional if it looked like this instead?
https://ongsho.com/contact
Why Remove .php From URLs?
Before jumping into the how, let’s talk about why you should consider removing .php from your URLs:
- Cleaner URLs: Clean URLs are easier to read and remember. Instead of /contact.php, users see /contact.
- Better SEO: Search engines prefer readable and keyword-rich URLs. Cleaner URLs often perform better in search engine rankings.
- Improved Security: By hiding file extensions, you reveal less information about your backend technologies, making it slightly harder for attackers to exploit known vulnerabilities.
- Framework-Like Behavior: Popular frameworks like Laravel, Symfony, or WordPress use clean URLs by default. Implementing this in custom PHP brings your setup closer to modern standards.
What You Need Before You Begin
Before removing .php extensions from your URLs, ensure you have the following based on your web server:
For Apache Users:
- A working PHP-based website hosted on Apache.
- Access to the .htaccess file in your site’s root directory (or virtual host configuration).
- mod_rewrite enabled on your Apache server.
- Permissions to restart Apache if changes require it.
- A basic understanding of Apache directory structure.
- Backup of your .htaccess or configuration files before making changes.
For Nginx Users:
- A working PHP-based website hosted on Nginx.
- Access to your Nginx server block configuration files (commonly in /etc/nginx/sites-available/).
- FastCGI and PHP-FPM properly set up and running.
- Permissions to restart the Nginx service after configuration updates.
- A basic understanding of the Nginx configuration structure.
- Backup of your nginx.conf or server block file before editing.
Organize Your File Structure
Make sure your files are located inside the correct root directory. For example, if you’re hosting your site at /home/cloudpanel/htdocs/ongsho.com/, your files should look like:
/home/cloudpanel/htdocs/ongsho.com/
├── index.php
├── about.php
├── contact.php
You want users to be able to access:
/about.php → /about
/contact.php → /contact
Step-by-Step Guide to Remove PHP Extension in Apache
Step 1: Enable URL Rewriting Module
If you’re using Apache, you must ensure mod_rewrite is enabled.
Run this command in your terminal (Ubuntu/Debian):
sudo a2enmod rewrite
sudo service apache2 restart
Step 2: Edit the .htaccess File
Go to the root directory of your website (e.g., /var/www/html/) and open or create the .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
This rule internally rewrites example.com/about to ongsho.com/contact.php while displaying only ongsho.com/contact in the browser.
Step-by-Step Guide to Remove PHP Extension in Nginx
Step 1: Open the Nginx Server Block Configuration
Locate your site’s configuration file, usually found in: /etc/nginx/sites-available/yourdomain.com
Or in the main config: /etc/nginx/nginx.conf
Step 2: Modify the location block
Find the PHP location block and replace it with:
location / {
try_files $uri $uri/ $uri.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock; # Adjust based on your PHP version
include fastcgi_params;
}
Replace php8.4-fpm.sock with your installed PHP version (e.g., php7.4-fpm.sock, php8.2-fpm.sock)
If you’re using TCP instead of socket: fastcgi_pass 127.0.0.1:9000;
This directive tells Nginx to look for .php files when a plain URL is requested.
Step 3: Restart Nginx
sudo nginx -t
sudo systemctl restart nginx
🔍 What does this mean?
- $uri → Try to find a file or folder that matches the exact URI (like /about)
- $uri/ → Try to match a directory (like /about/)
- $uri.php → If the file isn’t found, try appending .php (e.g., /about.php)
- ?$args → Append query parameters if they exist (/about?id=5)
🌐 Test Your Configuration
Now, try accessing the following URL:
https://ongsho.com/contact
If everything is configured correctly, it should display the content of contact.php without showing the .php extension.
You can test more routes like /contact
, /about
, etc., as long as their corresponding .php
files exist.
Optional: Redirect Old .php URLs to Clean URLs
To avoid duplicate content and SEO penalties, set up redirection:
Apache: Add to .htaccess:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\sHTTP
RewriteRule ^ %1 [R=301,L]
Nginx: In your server block:
rewrite ^/(.*)\.php$ /$1 permanent;
This will redirect ongsho.com/contact.php → ongsho.com/page with a 301 status. This is a good SEO practice to avoid duplicate content and ensure consistency.
🛑 Troubleshooting Tips
If something isn’t working, check the following:
- 404 Error
Make sure the .php file actually exists.
Double-check your root path in the server block.
Ensure permissions are correct (755 for folders, 644 for files). - PHP Not Executing
Check if your php-fpm service is running: sudo systemctl status php8.1-fpm
Make sure your Nginx config includes the correct fastcgi_pass path.
- Still Shows Homepage
This usually means try_files is falling back to index.php. Ensure $uri.php is correctly ordered before falling back to index.
- Top 25 Basic Linux Commands for Beginners
Final Thoughts
Removing the .php extension from your URLs is a simple yet powerful technique that enhances your site’s appearance, improves SEO, and boosts user experience. With just a few lines of Nginx configuration using try_files, you can modernize your PHP application and bring it closer to today’s development standards.
This method is particularly useful for:
- Custom PHP sites
- CMS platforms that don’t support routing
- Small business or personal websites hosted on VPS/cloud servers
At Ongsho, we use Nginx and CloudPanel to host fast, secure, and modern websites. If you’re looking to optimize your PHP projects or build something custom, try implementing this method in your own stack.
Need Help to Remove PHP Extension from Your URLs?
If configuring your server feels too technical or time-consuming, I can help you remove PHP extension from your website URLs quickly and professionally.
Whether you’re using Apache or Nginx, I’ll make sure your site has clean, SEO-friendly links.
Contact me today to remove PHP extension and make your URLs clean and modern!
📌 Summary
Removing .php extensions from URLs creates cleaner, more professional links—boosting SEO, improving user experience, and simplifying site maintenance.
In Nginx, a single line—try_files $uri $uri/ $uri.php?$args;—enables clean PHP routing with minimal effort, especially when using panels like CloudPanel.
Don’t forget to test your URLs and optionally redirect old .php links to maintain SEO and avoid broken links. Clean code, clean URLs—clean win!
Leave a Reply