• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Remove File Extension (.php, .html) from URL using .htaccess

Written by Rahul, Updated on May 8, 2020

The SEO experts says, there is no effect on SEO if your website URLs having .php, .htm or .html extension in there URL. Then why we need to remove these extensions from the URLs. As per experts says and my opinion that there are multiple benefits of not having file extensions in the URL, like:

  • Back-end technology is hidden from end users. But it’s still not hard to identify the technology for experts.
  • The best pros of this is that we can easily change backend technology without affecting SEO of the pages.
  • Read more about .htaccess here

To implement it, first you need to create a .htaccess file. Put it under the website document root. Then add the setting in the file as per your requirement to remove file extensions.

Removing .php Extension from URL

For example, You need to change URL from http://example.com/demo.php to http://example.com/demo. Edit .htaccess file and add following settings.

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

Now, If user accessed /demo in the browser, it will show the content from /demo.php. But still, if any user typed completed URL as http://example.com/demo.php, this will not redirect. Now you need to add some more rules to the .htaccess file.

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

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

Removing .html Extension from URL

For example you need to convert your url from http://example.com/demo.html to http://example.com/demo. Edit .htaccess file and add following settings:

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

Now, If user accessed /demo in the browser, it will show the content from /demo.html. Now, You may need to redirect users which typed complete URL as http://example.com/demo.html to the new URL http://example.com/demo

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

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

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

33 Comments

  1. Avatar jhon doe Reply
    January 7, 2021 at 6:37 am

    I AM JUST TESTING……………

  2. Avatar Ganesh Kumar Reply
    August 4, 2020 at 8:03 pm

    I am working html website my file name index.html but only show index ? Please help me which place to write a rewrite queries

  3. Avatar Hai Reply
    August 3, 2020 at 9:23 pm

    І am regilar reader, how aree you everybody? This post posted at this websute
    is truly pleasant.

  4. Avatar sagar wadke Reply
    April 29, 2020 at 12:58 pm

    Thanks for the very simplified article.

  5. Avatar Fey Reply
    October 12, 2019 at 3:21 pm

    Hi, Rahul.. thanks it works,

    but, what if :

    localhost/test/index.php
    to
    localhost/test/index.html or another ext.

    automatic rewrite rules same as the your content.. please help ! thanks before

    • Avatar sagar wadke Reply
      April 29, 2020 at 12:59 pm

      The code was provided in this blog only.

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

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

  6. Avatar buonlex Reply
    October 7, 2019 at 8:45 pm

    What if i want to remove also query strings from url?

  7. Avatar edi naba Reply
    July 16, 2019 at 6:19 pm

    cool.. thanks man

  8. Avatar lakshman Reply
    July 4, 2019 at 5:53 am

    Hi, In my .htaccess write below code for URL displaying purpose
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^request-quote?$ quote.php [NC,L]

    my URL now like https://mywebsite,com/request-quote this one show quote.php when google indexed old URL’s are like https://mywebsite.com/quote.php in this case we need URL change to same like “https://mywebsite,com/request-quote”. Any possibility I am struct here 16 hours I researched but no use please help me.

  9. Avatar John Doe Reply
    July 3, 2019 at 2:49 am

    I want to remove all extensions from URL.

  10. Avatar odinaka Reply
    April 28, 2019 at 4:41 pm

    worked, tnx man

  11. Avatar Deepak Reply
    March 27, 2019 at 7:30 am

    It working but with ajax i facing some variable value passing issue , (undefined warning )

    var dataString = ’email=’+ email + ‘&password=’+ password + ‘&jsremem=’+jsremem+ ‘&previousurl=’+previousurl;

    $.ajax({
    type: “POST”,
    url: “ajaxlogin.php?type=abc”,
    data: dataString,
    cache: false,
    success: function(result){ alert(result);
    }
    });

  12. Avatar Jafar Mohammad Musa Reply
    February 5, 2019 at 2:14 pm

    thank’s nice one its works 100%

  13. Avatar Amarjit Singh Reply
    January 17, 2019 at 9:47 am

    Thank you

    This really helps.

  14. Avatar Daryl Clark Abor Reply
    January 6, 2019 at 5:06 pm

    After searching for almost a couple of Hour. This setting worked well! Thank you

  15. Avatar abhishek Reply
    November 2, 2018 at 5:10 am

    first ofall thanks to give this code
    this is ver important and usefull code
    thanks very mouch

  16. Avatar USAMA ARSHED JADOON Reply
    July 23, 2018 at 11:11 am

    Thanks Man…
    You help me out…

  17. Avatar Ankit Dubey Reply
    June 14, 2018 at 7:52 am

    I Pasted The Code But Other Website Pages Are Not Working

    Ex-:when i open http://www.researchpanel.co.in/about-us it will open home page

  18. Avatar John Reply
    May 16, 2018 at 5:55 am

    Hi Rahul,

    If we have subfolder having different site, then url is not working.
    For example, http://test.com/ is my main site and I have other site on http://test.com/site2.

    So If I open http://test.com/site2 this url it shows me an error:
    The requested URL /site1/site2/.php was not found on this server.

    Do you have any solution for this?

  19. Avatar John Reply
    May 15, 2018 at 6:25 am

    Hi Rahul,

    Its working well with url but when I try to submit the form, I loose all the values. I cannot submit the form.
    What is the reason?

  20. Avatar Vinil Lakkavatri Reply
    April 17, 2018 at 12:18 pm

    Hi Rahul,
    Very easy to read and understand, however, I am having trouble in that it doesnt work for me, and I dont know why?
    Please help me if you can?
    Below is the url for the same where I want to remove .php extension
    http://localhost/mysite/api/users.php/users/abcdf/12345

  21. Avatar gili Reply
    February 18, 2018 at 9:04 am

    can you let me know how to remove PHP extension from specific php pages eg http://www.abc.com/contact-us.php

    How to redirect http://www.abc.com/contact-us.php to http://www.abc.com/contact-us

    • Rahul Rahul K. Reply
      February 19, 2018 at 6:07 am

      Hi Gili,

      The updated tutorial will help you more to achieve your tasks. For the single file, I hope below .htaccess rules should work for you.

      RewriteEngine on
      RewriteCond %{THE_REQUEST} /contact-us.php [NC]
      RewriteRule ^ /contact-us [NC,L,R]

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

  22. Avatar krishna Reply
    February 3, 2018 at 5:30 am

    thank you for this code
    but what to do if i want to remove .html extensionform URL like this “/WordPress/oepl/projecttask?p=project_detail.html&p_id=94836894-0813-xxxx-xxxx-xxxxxxxxxxxx”

    • Rahul Rahul K. Reply
      February 19, 2018 at 6:09 am

      Hi Krishna,

      What URL do you need after rewrite it?

  23. Avatar Ali Hammad Reply
    January 4, 2018 at 5:22 am

    I write this code but I still have a problem. When I manually type websitename/pagename.php it will not remove the extension (.php) please help to resolve this.

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

    • Rahul Rahul K. Reply
      February 19, 2018 at 6:07 am

      Hi Ali,

      I hope updated tutorial will help you more.

  24. Avatar Anto Navis Reply
    December 5, 2017 at 11:43 am

    Hi, Shall i know, its work on local host xampp server, i try it on local but not getting it, can you help to give the solution for it

    • Rahul Rahul K. Reply
      December 5, 2017 at 4:08 pm

      Please check for below directive in httpd.conf and set it to all.

      Allowoverride all

  25. Avatar KC Frank Reply
    October 31, 2017 at 12:43 pm

    It worked very well for me. Thanks

  26. Avatar Prakash Reply
    November 29, 2016 at 11:43 am

    Really good post i am going to use in my website url rewritting
    thanks

  27. Avatar Servis Racunara Reply
    February 5, 2016 at 11:53 pm

    how to remove both?

  28. Avatar Javed Ur Rehman Reply
    August 25, 2015 at 4:36 am

    I also wrote the small tutorial how to remove .php extension from url, hope you guys like it.
    https://htmlcssphptutorial.wordpress.com/2015/08/24/remove-php-extension-from-url-using-htaccess/

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy