Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Web Servers»Apache»Removing File Extensions from URLs using .htaccess (.php, .html)

    Removing File Extensions from URLs using .htaccess (.php, .html)

    By RahulFebruary 23, 20153 Mins ReadUpdated:January 13, 2023

    When it comes to creating a website, one of the most important things to consider is the user experience. This includes not only the design and functionality of the website but also the structure of the URLs.

    Advertisement

    A clean and simple URL structure can make it easier for users to understand the organization of the website and improve the overall navigation. Additionally, it can also help with search engine optimization (SEO) by making it easier for search engines to crawl and index your pages.

    The benefits of hiding file extensions:

    • 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 are that we can easily change backend technology without affecting the SEO of the pages.
    • Read more about .htaccess here

    One way to improve the structure of your URLs is by removing the file extensions, such as .php and .html. This can make your URLs appear cleaner and more professional, and can also help to hide the technology used to build the website.

    The process of removing file extensions from URLs can be done using the .htaccess file. The .htaccess file is a configuration file used by the Apache web server and can be used to make changes to the server’s behavior, including URL rewriting.

    Removing .php Extension from URL

    For example, You need to change the URL from http://example.com/about.php to http://example.com/about.

    Create a new `.htaccess` file or edit an existing one. This file should be placed in the root directory of your website. Add the following code to the `.htaccess` file:

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

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

    1
    2
    3
    4
    5
    6
    RewriteEngine on
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [NC,L,R]
     
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [NC,L]

    This code tells the server to turn on the rewrite engine and then checks if the requested URL is not a directory and if the file with a `.php` extension exists. If both conditions are met, it rewrites the URL by removing the .php extension.

    Removing .html Extension from URL

    For example, you need to convert your URL from http://example.com/about.html to http://example.com/about. Edit the `.htaccess` file and add the following configuration:

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

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

    1
    2
    3
    4
    5
    6
    RewriteEngine on
    RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
    RewriteRule ^ /%1 [NC,L,R]
     
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^ %{REQUEST_URI}.html [NC,L]

    It is important to note that removing file extensions from your URLs can have an impact on your website’s functionality. For example, if you have links or scripts on your website that are hardcoded with the file extensions, they will no longer work. Therefore, it’s a good idea to test your website thoroughly after making these changes to ensure that everything is working as expected.

    Additionally, you should also keep a backup of your original `.htaccess` file, in case you need to revert the changes.

    Conclusion

    In conclusion, removing file extensions from your URLs can make them appear cleaner and more professional, and can also help to improve the user experience and SEO. By using the `.htaccess` file to remove `.php` and `.html` extensions, you can make this change easily and effectively. Remember to test your website thoroughly after making these changes to ensure that everything is working as expected.

    Apache extension htaccess mod_rewrite rewriting url urls
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    CodeIgniter Remove index.php Using .htaccess

    Configuring the Nginx Reverse Proxy in Front of Apache

    Simple Redirects with .htaccess

    View 34 Comments

    34 Comments

    1. Md Zaheer Khan on August 8, 2021 5:47 pm

      Amazaing artical share. I can remove .php my site

      Reply
    2. jhon doe on January 7, 2021 6:37 am

      I AM JUST TESTING……………

      Reply
    3. Ganesh Kumar on August 4, 2020 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

      Reply
    4. Hai on August 3, 2020 9:23 pm

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

      Reply
    5. sagar wadke on April 29, 2020 12:58 pm

      Thanks for the very simplified article.

      Reply
    6. Fey on October 12, 2019 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

      Reply
      • sagar wadke on April 29, 2020 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]

        Reply
    7. buonlex on October 7, 2019 8:45 pm

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

      Reply
    8. edi naba on July 16, 2019 6:19 pm

      cool.. thanks man

      Reply
    9. lakshman on July 4, 2019 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.

      Reply
    10. John Doe on July 3, 2019 2:49 am

      I want to remove all extensions from URL.

      Reply
    11. odinaka on April 28, 2019 4:41 pm

      worked, tnx man

      Reply
    12. Deepak on March 27, 2019 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);
      }
      });

      Reply
    13. Jafar Mohammad Musa on February 5, 2019 2:14 pm

      thank’s nice one its works 100%

      Reply
    14. Amarjit Singh on January 17, 2019 9:47 am

      Thank you

      This really helps.

      Reply
    15. Daryl Clark Abor on January 6, 2019 5:06 pm

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

      Reply
    16. abhishek on November 2, 2018 5:10 am

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

      Reply
    17. USAMA ARSHED JADOON on July 23, 2018 11:11 am

      Thanks Man…
      You help me out…

      Reply
    18. Ankit Dubey on June 14, 2018 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

      Reply
    19. John on May 16, 2018 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?

      Reply
    20. John on May 15, 2018 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?

      Reply
    21. Vinil Lakkavatri on April 17, 2018 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

      Reply
    22. gili on February 18, 2018 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

      Reply
      • Rahul K. on February 19, 2018 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]

        Reply
    23. krishna on February 3, 2018 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”

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

        Hi Krishna,

        What URL do you need after rewrite it?

        Reply
    24. Ali Hammad on January 4, 2018 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]

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

        Hi Ali,

        I hope updated tutorial will help you more.

        Reply
    25. Anto Navis on December 5, 2017 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

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

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

        Allowoverride all

        Reply
    26. KC Frank on October 31, 2017 12:43 pm

      It worked very well for me. Thanks

      Reply
    27. Prakash on November 29, 2016 11:43 am

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

      Reply
    28. Servis Racunara on February 5, 2016 11:53 pm

      how to remove both?

      Reply
    29. Javed Ur Rehman on August 25, 2015 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/

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Error: EACCES: permission denied, scandir (Resolved)
    • How To Install Python 3.11 on Ubuntu 22.04 / 20.04
    • How to Install Python 3.11 on Amazon Linux 2
    • An Introduction to the “./configure” Command: Compiling Source Code in Linux
    • How to Install PHP 8.x on Pop!_OS
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.