How to Remove .php Extension from URL Using .htaccess
When you visit a website, the URL in the address bar often tells a story. A cluttered URL like meripariksha.com/contact.php can feel outdated, while a sleek meripariksha.com/contact looks modern and inviting. If your site runs on PHP and an Apache server, you can easily remove the .php extension from your URLs using the .htaccess file. This small tweak can improve user experience, make your site look polished, and even give it a slight edge in search engine rankings. Let’s explore how to do it.
Why Remove the .php Extension?
Removing the .php extension from URLs has several benefits:
- ✔ Cleaner URLs: Shorter, more user-friendly, and easier to remember.
- ✔ SEO Improvement: Search engines prefer clean URLs without file extensions.
- ✔ Security Enhancement: Hides your technology stack from potential attackers.
- ✔ Professional Look: Gives URLs a more polished and modern appearance.
What You’ll Need
Before we start, ensure you have:
- A website hosted on an Apache server.
- Access to your site’s root directory (via FTP, cPanel, or a similar tool).
- A text editor to modify files.
If you’re ready, let’s get started!
Step 1: Enable mod_rewrite in Apache
Before making changes, ensure that the mod_rewrite module is enabled on your Apache server. If you’re using XAMPP, WAMP, or a hosting provider, mod_rewrite is usually enabled by default. If you’re on a local server and it’s not working, check your Apache configuration (httpd.conf) and ensure this line is uncommented:
Step 2: Find or Create Your .htaccess File
In your website’s root directory (usually public_html or the main project folder), locate the .htaccess file. If it doesn’t exist, create a new file and name it .htaccess. If you don’t see it, don’t worry—you can create one [Create a new file and name it .htaccess (no extensions like .txt)].
Step 3: Edit .htaccess file
Add the following code to your .htaccess file:
How It Works:
- RewriteEngine On – Enables the rewrite engine.
- RewriteCond %{REQUEST_FILENAME} !-d – Ensures the request is not for a directory.
- RewriteCond %{REQUEST_FILENAME}.php -f – Checks if the requested file exists as a .php file.
- RewriteRule ^([^\.]+)$ $1.php [NC,L] – Redirects requests without .php to the corresponding PHP file.
Step 4: Test Your URLs
Save the .htaccess file an Open your browser and type a URL without the .php extension (e.g., meripariksha.com/contact). If everything’s set up correctly, the page should load as if you’d typed meripariksha/contact.php. If you get a 404 error, double-check your code or confirm mod_rewrite is enabled on your server (contact your host if unsure).
Step 5: Update Internal Links (Optional)
To make this fully effective, update your internal links:
❌ Before:
✅ After:
This ensures users don’t see .php in the URLs.
Step 6: Redirect Old URLs (Optional for SEO)
If your website has already been indexed with .php URLs, use 301 redirects to prevent broken links:
This ensures that search engines and users who access meripariksha.com/page.php are redirected to meripariksha.com/page.
Why It Matters
Removing .php from URLs isn’t just cosmetic. It makes your site easier to navigate, hides the backend technology (improving security perception), and aligns with modern web standards. Search engines might also appreciate the simplicity, giving your pages a slight ranking boost.
Troubleshooting Tips
- 500 Internal Server Error: Check for typos in your .htaccess file or ensure mod_rewrite is active.
- URLs Not Working: Verify file permissions (.htaccess should be 644) and that your PHP files exist.
- Mixed Results: Clear your browser cache after changes.
With just a few lines of code, your URLs can go from functional to fantastic. Give it a try and enjoy the cleaner look!
By following these steps, you can successfully remove the .php extension from your URLs, improving both usability and SEO. 🚀