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.
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.
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.
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.
now you have a url like this : http://www.yoursite.com/login
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/
That’s it! hope you guys like this post and please don't forget to share.
Let's Start
read here official .htaccessAn .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 urlhttp://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 https://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.
not working code
ReplyDeleteplease let me know, how it's not working ?, put this code in your root project folder and remove extensions from url, it will work.
DeleteDamn this is good. Thank you. found your site last night.
ReplyDeletethank you :)
DeleteThank you so much!
ReplyDeleteyou're welcome, brad :)
Deletekeep visiting...
superb....... thanks broo.....
ReplyDeleteyou're welcome :)
DeleteThanks a lot! It is brilliant for my project
ReplyDeleteHello Jhon, thanks for the valuable comment. do visit again :)
DeleteThanks alot
ReplyDeletehow to get
ReplyDeletesubdomain.domain.com/file.php
to
subdomain.domain.com/file
hello ishank, use the following code
DeleteRewriteRule ^(.*)$ $1.php
and how to change url name ? like
ReplyDeletelocalhost/index.php?page=view&id=1 to view/1/filename
Hello Aris, use the following code
Delete<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>
how to use it on live hosted site on godaddy server??
ReplyDeleteemail - [email protected]
Hello Nike, you can use it just host this file on your server but in godaddy server some changes may require
Delete#Fix Rewrite
Options -Multiviews
put the above code before your : mod_rewrite directives
the script download link not working now?
ReplyDeletedownload this script again ...
Deletethe script download link is not working
ReplyDeleteHi Ahmed, download it again now it's working :)
DeleteGreat work...your blog explains everything in detailed yet simple manner. keep up your good work
ReplyDeleteHello Charmer,
Deletethanks for such a nice comment, keep visiting :)
Amazing work perfectly fine , thanx buddy :)
ReplyDeleteHello Deepak,
Deletethanks for dropping comment. do visit again :)
you are good
ReplyDeleteYou are good
ReplyDeletethanks abubakar, :)
DeleteHello Pradeep,
ReplyDeleteThank 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
hello there, use google webmaster to crawl your pages.
DeleteYou have the best coding website ever!! Thank you so much! This has helped my website indescribably!
ReplyDeleteThank You
ReplyDeleteYou're welcome, Maxamed :)
Deletecss and js not working when I add slash(/) at the end of url. How can I solve it, please.
ReplyDeletehi, eiphyolin,
Deletejust use absolute path on links, that's it ...
I want to remove .html from my file stuff.html.
ReplyDeleteshould i put:
RewriteCond %{REQUEST_stuff.html} !-d
RewriteCond %{REQUEST_stuff.htm}.html-f
or
what?
Brilliant, so much nicer.. THanks a lot
ReplyDeleteThank Youuuuu Pradeep...
ReplyDeleteYou're Welcome, Suren
DeleteHi Pradeep!
ReplyDeleteGreat 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!
i have seen many answers but am still not getting one part how do i remove the extension
ReplyDeletethis 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
this is very helpfull thanks
ReplyDeleteglad it helps you, :)
DeleteHii... how can i remove id to this link. http://trustedbusiness.reviews/details.php?id=3
ReplyDeletethis's very HelpFull ; thanks Sir
ReplyDeletenot working well .. i just follow your instruction ..
ReplyDeletelink: 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
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 ?
ReplyDeleteMuy bueno, muchas gracias por el aporte... :)
ReplyDeleteHi pradeep
ReplyDeleteI 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.
Hi. I like the code but I have a few questions.
ReplyDelete1. 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
super duper solution.
ReplyDeleteGreat Great. I was looking for this for last 6 years.
Hey, Jai Sundar thanks for dropping your comment, keep visiting. :)
DeleteNice post. How will i remove http://mysite.com/update.php?id=415 to http://mysite.com/the name of the article.thanks
ReplyDeleteNot working. Gave me a 404 throughout the entire site.
ReplyDeletePerhaps it's only good for PHP5.3 and below
Not working, gave me a 404 throughout the entire site.
ReplyDeletePerhaps only good for PhP versions 5.3 and below.
RewriteEngine On
ReplyDeleteRewriteRule ^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
Hey Anurag, just use absolute urls for css/js it will work :)
Delete