How to Remove .php, .html Extensions with .htaccess File | Coding Cage

How to Remove .php, .html Extensions with .htaccess File

By
hello friends, in today's tutorial you will learn how to remove .php and .html file extensions from URLs using .htaccess file, you might have seen that some sites did not displays .php or .html extension even they are created in core, normal php, you can also do the same for your site or projects using .htaccess file, recently i was working on my project and i wanted to remove the extensions from my website, in order to make the URLs more user friendly. so i was thinking why not to share this simple .htaccess tip with you, well this was first .htaccess post on my blog and i will post some more tips and tutorials on this blog stay tuned, let's have a look.
How to Remove .php, .html Extensions with .htaccess File

Let's Start

read here official .htaccess
An .htaccess file is a simple ASCII file that you can create with a text editor like Notepad or Notepad++ . It provides a way to make configuration changes on a directory/folder. instead of this we can do more thing with .htaccess file like Redirect the user to different page, Password protect a specific directory, Rewrite URI etc. we will see it next time.

remove .php extension

In order to remove .php extension from your site url, for example suppose you have following url
http://yoursite.com/signup.php
and you want url like this
http://yoursite.com/signup
so which one is better, of course the second, now to achieve this you have to put following code inside .htaccess file so create a new file using any text editor and save it as (.htaccess) but one thing you should keep in mind that the .htaccess is the file’s extension, following code is for .php extension.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

after it create a new file as login.php and save it in your root folder with where you have saved .htaccess file, then you can access that URL without .php extension, try it.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Remove .php extension with .htaccess by http://www.codingcage.com/</title>
         
        </style>
    </head>
<body>
 
<h1>This is login.php page | <a href="login">click here</a> to remove .php extension and see the url in addressbar</h1>

<a href="signup">signup here</a>
 
</body>
</html>

now you have a url like this : http://www.yoursite.com/login

remove .html extension

do the same as i showed above but you simply have to alter the last line from the code above to match the filename:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html-f
RewriteRule ^(.*)$ $1.html
</IfModule>

Adding a trailing slash at the end

ok we have done it, now how about adding trailing slash at the end of the url like this.

http://yoursite.com/login/

if you want this just replace "RewriteRule ^(.*)$ $1.html" code with this "RewriteRule ^([a-z]+)\/?$ $1.html [NC]" and you are done and you have url like this :

http://yoursite.com/login/

final .htaccess code to hide .php , .html


<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
#RewriteRule ^([a-z]+)\/?$ $1.php [NC]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
#RewriteRule ^([a-z]+)\/?$ $1.html [NC]

</IfModule>
You can now link your html and php pages inside the HTML document without .php, .html extension of the page. For example:
<a href="http://yoursite.com/login">login</a>

That’s it! hope you guys like this post and please don't forget to share.



57 comments:

  1. Replies
    1. please let me know, how it's not working ?, put this code in your root project folder and remove extensions from url, it will work.

      Delete
  2. Damn this is good. Thank you. found your site last night.

    ReplyDelete
  3. Replies
    1. you're welcome, brad :)
      keep visiting...

      Delete
  4. superb....... thanks broo.....

    ReplyDelete
  5. Thanks a lot! It is brilliant for my project

    ReplyDelete
    Replies
    1. Hello Jhon, thanks for the valuable comment. do visit again :)

      Delete
  6. how to get
    subdomain.domain.com/file.php
    to
    subdomain.domain.com/file

    ReplyDelete
    Replies
    1. hello ishank, use the following code

      RewriteRule ^(.*)$ $1.php

      Delete
  7. and how to change url name ? like
    localhost/index.php?page=view&id=1 to view/1/filename

    ReplyDelete
    Replies
    1. Hello Aris, use the following code

      <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&id=$[id]
      </If>

      Delete
  8. how to use it on live hosted site on godaddy server??
    email - nike2207@gmail.com

    ReplyDelete
    Replies
    1. Hello Nike, you can use it just host this file on your server but in godaddy server some changes may require

      #Fix Rewrite
      Options -Multiviews

      put the above code before your : mod_rewrite directives

      Delete
  9. the script download link not working now?

    ReplyDelete
  10. the script download link is not working

    ReplyDelete
    Replies
    1. Hi Ahmed, download it again now it's working :)

      Delete
  11. Great work...your blog explains everything in detailed yet simple manner. keep up your good work

    ReplyDelete
    Replies
    1. Hello Charmer,
      thanks for such a nice comment, keep visiting :)

      Delete
  12. Amazing work perfectly fine , thanx buddy :)

    ReplyDelete
    Replies
    1. Hello Deepak,
      thanks for dropping comment. do visit again :)

      Delete
  13. Hello Pradeep,

    Thank you for the code. I used it on my site and it works perfectly.
    I have an issue though and I thought you might be able to help me out.
    Let me explain:

    So, I have the following pages on my website:
    www.example.com/demo/test.html
    www.example.com/demo/another.html
    www.example.com/demo/page.html

    I used your code to remove the .html extension, and all is good.
    But, as my website is old, the page test.html is already indexed in Google.
    The other two pages are new, I just added them, so they are not yet indexed by Google.

    For SEO reasons I want to 301 redirect the ‘test.html’ page to the ‘test’ page.
    In this way I will ‘tell’ Google that “the ‘test.html’ that you have stored in your index, is no longer ‘test.html’ but ‘test’”
    How do I do that?
    I have searched and searched and I cannot find an answer.
    Could you please help me?

    Thank you very much in advance
    Olga

    ReplyDelete
    Replies
    1. hello there, use google webmaster to crawl your pages.

      Delete
  14. You have the best coding website ever!! Thank you so much! This has helped my website indescribably!

    ReplyDelete
  15. css and js not working when I add slash(/) at the end of url. How can I solve it, please.

    ReplyDelete
    Replies
    1. hi, eiphyolin,

      just use absolute path on links, that's it ...

      Delete
  16. I want to remove .html from my file stuff.html.
    should i put:
    RewriteCond %{REQUEST_stuff.html} !-d
    RewriteCond %{REQUEST_stuff.htm}.html-f
    or
    what?

    ReplyDelete
  17. Brilliant, so much nicer.. THanks a lot

    ReplyDelete
  18. Thank Youuuuu Pradeep...

    ReplyDelete
  19. Hi Pradeep!

    Great explanation and so nice to have files to test with! Unfortunately I am not seeing the trailing slash when I replace this line of code: RewriteRule ^(.*)$ $1.php with RewriteRule ^([a-z]+)\/?$ $1.php [NC]

    Here's my file. Any ideas to what I am doing wrong?

    #Fix Rewrite
    Options -Multiviews


    RewriteEngine on


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


    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^([a-z]+)\/?$ $1.html [NC]





    Thanks!

    ReplyDelete
  20. i have seen many answers but am still not getting one part how do i remove the extension
    this is how am thinking
    i have this link
    www.myhome.com/family.html
    the file family.html is in my root folder
    so at what point in time and how do i remove the extension am new in this coding field just learning online
    please take me through all the steps 1 by 1
    thank you

    ReplyDelete
  21. this is very helpfull thanks

    ReplyDelete
  22. Hii... how can i remove id to this link. http://trustedbusiness.reviews/details.php?id=3

    ReplyDelete
  23. this's very HelpFull ; thanks Sir

    ReplyDelete
  24. not working well .. i just follow your instruction ..

    link: http://localhost/remove-extension/login

    http://localhost/remove-extension/login.php <-- i use like this my server running up..

    Not Found

    The requested URL /remove-extension/login was not found on this server.

    Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80

    ReplyDelete
  25. Thanks alot PRADEEP KHODKE. I always refers on your tuts. Can i use it on CPanel directly or is there something to be done to make it work on my site ?

    ReplyDelete
  26. Muy bueno, muchas gracias por el aporte... :)

    ReplyDelete
  27. Hi pradeep
    I am using this code on godaddy server:

    Options -Multiviews
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html

    and my html code is:
    ABOUT
    But it is not working.
    Please help me out?
    Thanks in advance for ur great work.

    ReplyDelete
  28. Hi. I like the code but I have a few questions.
    1. I'm worried that it may affect my Google rankings.
    For example, my page www.mysite/wickets.html ranks well in Google, but if I use the .htaccess file to change the url to www.mysite/wickets, will this keep the same ranking I had previously?
    2. If someone types into the address bar www.mysite/wickets.html, will it automatically go to the correct page or do users have to type in www.mysite/wickets?
    3. I have a page www.mysite/payments.html and also a subfolder called payments with a file index.html in it. If I use the .htaccess file, which page will it go to if someone types www.mysite/payments in the address bar?
    Will it go to mysite.payments.html or mysite/payments/index.html?
    Thanks in advance

    ReplyDelete
  29. super duper solution.
    Great Great. I was looking for this for last 6 years.

    ReplyDelete
    Replies
    1. Hey, Jai Sundar thanks for dropping your comment, keep visiting. :)

      Delete
  30. Nice post. How will i remove http://mysite.com/update.php?id=415 to http://mysite.com/the name of the article.thanks

    ReplyDelete
  31. Not working. Gave me a 404 throughout the entire site.
    Perhaps it's only good for PHP5.3 and below

    ReplyDelete
  32. Not working, gave me a 404 throughout the entire site.
    Perhaps only good for PhP versions 5.3 and below.

    ReplyDelete
  33. RewriteEngine On
    RewriteRule ^about-us/?$ about.php [NC,L]

    when i remove .php then my website design will be shake. when i put www.abc.com/about.php then everything is ok. what when i put www.abc.com/about-us/ website design will be shake. i think css not working

    ReplyDelete
    Replies
    1. Hey Anurag, just use absolute urls for css/js it will work :)

      Delete