I was working on a (rather neglected, outdated) site I had previously done and decided it needed some (rather, a lot of) improvements. I was looking for a way to get rid of to clean up the URLs without having to remap my files. .htaccess to the rescue!
.htaccess
files are a wonderful thing, and incredibly powerful, ignoring the bad experience I had with them on my current web host (fortunately, this project is hosted by another web host, and everything runs perfectly with them :)), and Apache's mod_rewrite
module does exactly what we need.
There are a bunch of reasons you'd want to do this, mainly:
- it looks cleaner and easier to read and remember
- by using search-engine–friendly URLs, you're Google PageRank will increase and will increase your website's findability
- it makes your website environment-independent, so if you ever decide to change the technology your site uses, everything would appear seamless to your visitors.
In this case, we're using PHP files, but you can change it to whatever type of file you're using, be it .html
, .asp
, .cfm
, or anything else, as long as they're all the same type. (If you want to do this for multiple file types, just copy lines 2–4 and apply the same technique accordingly.)
Open your text editor and create a file called ".htaccess
" with the following code in it, and upload it to your site's root directory (Note: On Unix and unix-like operating systems, files that start with a dot are hidden files, so you may not be able to see the file after you save it. To get around this, omit the preceding dot when naming the file, and then rename the file back to ".htaccess
" after you have uploaded it to your webserver):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
(adapted from the Apache mailing list)
Using this code, instead of having to type in http://mysite.com/contact.php
, you only need to enter http://mysite.com/contact
to access that page. And the best part is, you can still access the page with .php
on the end of it, so no old incoming links or bookmarks become orphaned as a result of this, and everyone is happy.
92 comments:
Nice. Thanks.
nice article... :-)
thanks for your help man!!!
thx dude... been searching for a while for this!!!
As Monty Burns would say, "Excellent!"
Thank you so much. I've been tying to figure out how to do this for the past week. With .htm files.
Thanks
Rayvin Nyte
Guild of Abundance
hi,
Could u tell me what u have done that ur script works fine.
I have added ur script in my .htaccess file but it is not working, it is giving error
Error 404: NOT FOUND!
Your browser cannot find the document corresponding to the URL you typed in.
for RewriteCond specify the website root directory:
/website root dir/%{REQUEST_FILENAME} ...
Thanks! I've been trying to figure that out. You really saved me a lot of time.
Hi
Just remember that if you have a link called myportfolio.php and a folder called myportfolio/ which contains your portfolio work then this might not work. So you have 2 options you either add in some complex regexp/re write rules or.... make sure you dont have the same folder names as href links! Enjoy!
Re: directory and filenames being the same causing problems - it's a good thing to point out, but it's bad practice to have that. Directories and filenames should be different. If you have a file that exists in a directory, it should be called index, not the same name. Otherwise, users accessing that directory would be able to see your file list. As for the .htaccess solution itself, THANK YOU. So, so much. This worked without a hitch. Much appreciated.
GREAT WORK AND THANK YOU VERY MUCH! Was looking everywhere for this and couldn't find it anywhere! Many thanks again!
great! thanks for this tip, I had been searching for a solution for hours!
I use 1and1.com and can't seem to get this to work right?
I am getting the 404 error like "Praveen Kumar Gupta" and have tried the advise of "PoloMercantil said...
for RewriteCond specify the website root directory:
/website root dir/%{REQUEST_FILENAME} ..."
but I can't seem to get that right either?
I use Linux Hosting and already have a .htaccess file that I use to forward to my custom 404 page, so I know I have one, and know that it works. In "PoloMercantil"'s remarks they said to specify the "Website Root Directory" and gave the example. Since I use Linux hosting it is simply / and my specific website is in a folder called UCHT so how exactly would I correctly write the above example to get it to work? and would I place it in the UCHT folder, or just the / directory? Also the "%{REQUEST_FILENAME}" appears twice in this code, so do I put the directory info on both lines/both times? (I have tried literally like 20 different combinations and guesses to get this to work, ( /%{REQUEST_FILENAME}", ///%{REQUEST_FILENAME}", /UCHT/%{REQUEST_FILENAME}" with spaces without spaces. on one line/on both lines/root directory/website folder...) but I just can't get it to work???)
Also can I repeat this over and over for .php and .html pages in the same .htaccess file? just seperate them by a "Return/Line" and then change the example to .html in both places that say .php?
Thank You SO much, anyone who has the answer to this.
Thx 100 times, I use it to remove the ".html" which works perfectly, by changing the "php" to "html"
You are great, thanks again
I am creating a new site. All the files I have created are HTML, but the Contact form (which uses Ajax) is PHP.
I would like to have all the files extensions hidden via htaccess.
eg.
.com/about.html > .com/about
.com/contact.php > .com/contact
Is there any way I can do this? Furthermore, this is a brand new site, so I would like to hide the direct file (ie. you can only access .com/about - not .com/about.html) - can I use 301 redirects to redirect about.html to /about - or will this confuse the htaccess script?
Works perfectly on our site: www.thabto.co.uk
...Thanks very much
Brilliant .... Thank You
any idea, how to achieve the same thing in IIS 6/7
I tried above code for .shtml files and it's not working. Any idea?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.shtml -f
RewriteRule ^(.*)$ $1.shtml
8/24/2009 12:40:00 PM
I will try this straight away!
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.asp-f
RewriteRule ^(.*)$ $1.asp
im trying with asp, its not working
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.asp -f
RewriteRule ^(.*)$ $1.asp
im trying the above code, its not working, any idea...? Please help me out ... :)
Thanks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.asp -f
RewriteRule ^(.*)$ $1.asp
Iam using the above code, its not working...something wrong...? Please help me out :)
Hey Admin,
You need to remove all ".asp" links from all ur pages,
Then it should work!! :) cheers!
I've tried this code and it is not working for me.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I've even put the file in every place possible (root, folders, etc.) Am I doing something wrong?
Hey Candy, do you have any ".html" urls within ur pages?
I want to fix it so that if users type /xxx.html they get /xxx, and I thought this method would work, but it doesn't seem to. Do I have to do something different? And is there a way to do without gutting this extension stripper?
THANK YOU!
Hi,
This is a great tip.
Here, the file is accessing in both the ways: http://mysite.com/contact.php and http://mysite.com/contact
This is a URL canonical issue. How to solve this canonical issue. I need, even if anybody search with the extension also it must be redirect to http://mysite.com/contact
Finally, file must have to be accessed only for the URL: http://mysite.com/contact
How to do this?
Thanks again,
Karunakar Rao Pokala.
Nice, simple, easy...and it worked!
Thank you!!!
For the people having issue's with this not working, make sure you remove the .html, .php, .htm...etc is removed from the link in your html.
EX
Bad:# href="/about.html" title="About Me">About Me
good:# href="/about" title="About Me">About Me
does not work on godaddy hosting services!! used the .htaccess file on MAMP and it worked but still having issues with godaddy. their support center was no use...
seems there is a glitch ie:
if you type
mysite.com/test.html
it of will take you to mysite.com/test
but if you type
mysite.com/test/ it for some reason turns off the css.
why is that?
seems there is a glitch ie:
if you type
mysite.com/test.html
it of will take you to mysite.com/test
but if you type
mysite.com/test/ it for some reason turns off the css.
why is that?
seems there is a glitch ie:
if you type
mysite.com/test.html
it of will take you to mysite.com/test
but if you type
mysite.com/test/ it for some reason turns off the css.
why is that?
seems there is a glitch:
if you type
mysite.com/test.html
it of will take you to mysite.com/test
but if you type
mysite.com/test/ it for some reason turns off the css.
why is that?
Thanks for this great tip.
1 question - when I manually type the adress it works, but when I press a link to another page in my site it will show the page with the extension, jow can I hide it?
thank you for making that so simple! been looking for that fix forever. (feels like forever anyway)
cheers,
erin
thank you so much|!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! i was looking for this.....thanks!
Does exactly what it says on the tin
Top Job!
Works on my PHP pages, dude.
I changed the links in my php / html omitting the extension. Now when I type example.com/test it works, example.com/test.php it works, when i do example.com/test/, it throws a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request. How to fix this?
This blog has been helping people for 3yrs now. Nice job my man!!
This is tricky.. WHAT IF THERE IS A FOLDER HAVE A SAME NAME AS A PHP FILE IN THE SAME DIR???
Love your post, I even linked to it in one of mine: Hide File Extension with .htaccess
Thanks alot, worked very well for me!
How can i rewrite a rule like this:
http://mysite.com/page.php?content
into this:
http://mysite.com/page/content/
?
I'm new using rewritecond and i can't find an answer to this...:(
Thanks in advance
@GB: try this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^subfolder/([0-9]+)-([a-z]+) http://mysite.com/subfolder/page.php?content=$1-$2 [NC]
it would display http://mysite.com/subfolder/content for http://mysite.com/subfolder/index.php?content=something
hope this helps
@GP: sorry, more precisely it would display http://mysite.com/subfolder/something for http://mysite.com/subfolder/page.php?content=something
Thank you very much for your advice webeno!..
actually, the subfolder part is useless.
take a closer look in my example:
http://mysite.com/page.php?content
into this:
http://mysite.com/page/content/
any ideas?
(thank you!)
im trying to hide all the .php extensions on my website... this is the .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
but its not working... when i click eg.
www.example.com/about it works but if i click it from the navigation menu the .php still shows? please help?
im trying to hide all the .php extensions on my website... this is the .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
but its not working... when i click eg.
www.example.com/about it works but if i click it from the navigation menu the .php still shows? please help?
What's the way around this if you're using a server like hostclear and it doesnt seem to work. Am I correct in saying this .htaccess file should be literally just dropped into the public_html (assuming that's where the site is published to) and let it do its work? Cos Ive tried the above suggestions and only get error 500s :S
I'm not the admin of this blog, so hope he's not offended if i answer :)
@ Sachin: not sure how your site is built exactly, but if you click on a link that leads you to pages with .php extension at the end, it will display the extension in the address bar too. You have to make sure that you remove the extension from your links so that they don't display.
@ Tom: you're absolutely right, however some hosting providers don't allow .htaccess. if you get the 500 error after setting up the .htaccess file, then it's allowed and you made a mistake in your file. If the error is there before adding .htaccess, something else must be wrong there and you better contact your hosting provider for help.
If the URL is still accessible with the .php extension, Google has two versions, no? That means that there is duplicate content en that will be punished...
If the URL is still accessible with the .php extension, Google has two versions, no? That means that there is duplicate content en that will be punished...
thankxxxxxxxxxxxxxxx... i waz looking for this kind of thing from many day .. g8 work man..
Worked perfectly, had a problem with the host One.com who told me I would have to create a subdomain for every page and point it the document. Otherwise I would have had to change 30 odd links manually, thanks again =)
Does is work with 1and1.com beginner package? Is there a specific technique required while putting that on ftp server?
thanks..usefully..
Very Nice tip.... really cool method to hide extension of files...
well I am going to try... if I will stuck anytime.. I will tease you by asking the solutions... lol :P
Keep up good work :)
Thnx
Help!
Keep getting 500 error messages, how can you find the hidden .htaccess file to delete it??
Have selected all in the folder except cgi-bin and deleted, but hasn't worked, also tried searching for it but no luck, have three sites down now, using FN Hosting and Transmit...
Phew, sorry, slight panic..
TIPS...
For anyone in the same boat go to VIEW in Transmit and click on Show HIdden Files, to see the ,htaccess on the server.
And the 500 error messages were because TextEdit on mac was saving the htaccess as a RTF RichText file... it must be plain text and saved as a .txt file (got rid of 500 error messages)... oh, and have deleted all .html extensions from the links.... works a treat now.. Thank you!!! x
it has been working for me so far but this piece of code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
appears as invalid here:
http://www.lyxx.com/freestuff/002.html
why is this happening?
Hi,
I am trying to remove the .html extension from my website but I haven't had much success.
I have listed the steps below that I have taken.
1. The file is an .htaccess.txt file
2. I have deleted the .html from a few of the links to test it out but I am getting file not found messages when it does try to link.
3. I have asked my hosting provider (mecca hosting) to enable .htaccess.
4. I have put my root folder in the .htaccess file code so the code is now
RewriteEngine on
RewriteCond /http://www.juliamcpherson.co.uk/%{REQUEST_FILENAME} !-d
RewriteCond /http://www.juliamcpherson.co.uk/%{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
I am really lost and not really sure what to do to fix it as I am pretty new to this.
Hopefully someone can help?
yaayy!!
haha lol yz http://www.vinica.org/images/dl.php?fileid4643-Photo32.JPG
That didn't work for me. Although, I tried removing the .htm...
I tried this, later:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.htm [NC,L]
Still not working. Why? I wonder...
Hii
I am having a website in a folder name site .I save the following code in this folder in .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
but it's not working ..plz provide me valid solution or guide the the valid way to do this.I am not able to understand
thanks apreciated
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.asp -f
RewriteRule ^(.*)$ $1.asp
guys can you give me a example
RewriteEngine on
RewriteCond %eg !-d
RewriteCond %eg\.html -f
RewriteRule ^(.*)$ www.eg.com/eg.html
this is correct
Thanks man!
Email hosting is provided by companies large and small. Such well-known companies as Yahoo, Google, and Microsoft offer email hosting.
@ karunakar :
You found a solution? I have the same probelm. Thanks karunakar
Thanks so much! I've been looking everywhere for this!
Works perfectly on our site: www.thabto.co.uk
...Thanks very much
super duper like.
this is a perfect example.
so simple to implement.
just took 2 min , and i was searching it for many days.
Lucky I am
Kidsmitra.com
Guys
please visit www.kidsmitra.com
for all kind of kids related information
ex. tuition,play schools,schools,yoga classes, doctors and many more
thanks
Manu
Thank you so much! I've been looking everywhere for this and nothing would work. Thanks again!!
great post know about Sunrise Health Resorts in Jaipur and water park in Jaipur, Have developed a special 7 days programme called Anti-Ageing, based on Ayurveda centre in Jaipur & ayurveda hospital in Jaipur,ayurveda treatment in Jaipur, Yoga centre in Jaipur and Naturopathy treatment in Jaipur; these ancient systems of wellness centre in Jaipur. contact-9828590094
all rajasthan
seo training institute in Jaipur
seo service in Jaipur
spa in Jaipur
spa centre in Jaipur
rajasthan government jobs
water park in Jaipur
Get the list of best web hosting companies which provide excellent services and support for your personal or small business websites here : Best Web Hosting Companies
Добрый вечер!
Даже очень прикольный сайт.
Много приколов...Дудка, Спайс, Анаша, План, и т.д.
Переходи на сайт:
[b]
http://gidra.ukadudyraf.tk
[/b]
Hi! An excellent blog that describes all Hide file extensions in URLs with .htaccess files of the blog in detail. If you want to know about server hosting or are interested in the best Italy VPS Hosting you can ask us for more details and services. Get the best and fast hosting service with us.
This blog describes all Hide file extensions in URLs with .htaccess files of the blog in detail. If you want to know about or search for hosting services then go for Europe VPS Server for excellent services.
Amazing information about hiding file extensions in URLs with .htaccess files. So, if u interested in dedicated server hosting you can ask us for more information and updates. Visit-
Greece VPS Server
Thank you for sharing your post, it is very useful for us.
Are you looking for a server hosting Visit us for exploring Malaysia Dedicated Server at the lowest price.
Really Grateful for providing such technical information about .htaccess files. You know what, I really enjoyed the information you shared in this blog. I want to share about Brazil Dedicated Server for those who are looking for a server that provides unlimited bandwidth and optimum security. So, if anyone is interested just check out our affordable hosting plans.
I found this articel very informative which is about Hide file extensions in URLs with .htaccess files. If you want to host your website then go for Europe Dedicated Server
Thank you for share this is such a very nice post I really like your blog. Wonderful publish, very informative. You show you an alternative way to do the same using SQLCLR (SQL Common Language Runtime) which is a technology for hosting the Microsoft .NET common language runtime within SQL Server. If you want to know about server hosting or interested in the best Russia VPS Server Hosting you can ask us for more details and services. Join Onlive Server to give a new way to your business.
Post a Comment