1

I've been trying to figure out how to remove .php extension, I've searched everywhere and it seems most of the code are not working anymore. The code below is what I am using now to remove the .php extension but it is not working.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !- f
RewriteRule ^([^\.]+)$ $1.php [NC]
Cœur
  • 37,241
  • 25
  • 195
  • 267
Bono
  • 513
  • 2
  • 7
  • 10

3 Answers3

5

I'm using XAMPP on Windows 10. I also tried many times to remove the .php extension but the result was the same. But after a long while finally, I got how we can do this. If you want the same results, just follow the steps given below...

To remove the .php extension from a PHP file, e.g. if you want to change mywebsite.com/about.php to mywebsite.com/about you have to add the following code inside your .htaccess file:

First of all, make sure you've created a new file named .htaccess and placed it at your root-level/root-directory where your index file is placed. Now, open this (.htaccess) file with any editor of your choice, copy/paste the given code and save it...

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

Now, if you (any user) will access mywebsite.com/about in the browser, the user will see the content of mywebsite.com/about.php page.

But still, if you (any user) will access the URL as mywebsite.com/about.php, this will not redirect the user to this mywebsite.com/about page. But will go to this mywebsite.com/about.php page which means the user can still visit mywebsite.com/about.php page. Don't worry about it. If you want to avoid this, you can simply follow the next step.

To avoid the above problem, now you need to add some more rules in the .htaccess file. For this, you've to replace your old code with this new one given below, save your file and check it out...

RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

That's all and you're all set. I hope this will fix everyone's problem.

Have a nice day :)

Tabassam Ali
  • 232
  • 4
  • 12
  • Kindly visit this answer... https://stackoverflow.com/a/69050795/15639663 ... if you want to know how to remove the **`.html`** extension. – Tabassam Ali Sep 03 '21 at 21:50
1

To remove the .php extension from a PHP file

for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:

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

Or

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

Or

see removing .php extension from URL

Also, make sure that you've mod_rewrite on.

Also see how to create .htacess file

Community
  • 1
  • 1
Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
  • It is still not working, I get an error "The requested URL "http://localhost/series3/list" cannot be found or is not available. " – Bono May 12 '14 at 06:46
  • make sure that you've mod_rewrite is on. – Rakesh Shetty May 12 '14 at 06:47
  • How do I change it to on? – Bono May 12 '14 at 06:49
  • see [How to enable mod_rewrite](http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2) – Rakesh Shetty May 12 '14 at 06:51
  • 2
    It is still not working I get the following error "The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. Here is my full code RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L] If you think this is a server error, please contact the webmaster." – Bono May 12 '14 at 06:54
  • @Bono did you checked is mod_rewrite is enabled or not ? – Rakesh Shetty May 12 '14 at 06:56
  • I added RewriteEngine On to enable the mod_rewrite – Bono May 12 '14 at 06:58
1

I think you need to change internal website coding as well with htaccess code above and remove the file name URL extension from where it is mentioned in your web coding. Eg:-

https://yoursite.com/page.html

changed to

https://yoursite.com/page/

htaccess code

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html [NC,L]