![]()
I recently updated part of my website and made it dynamic (transited from html to php). I stumbled over the problem how to remove the .php extension from a php page. In this tutorial I’ll show you how to do it easily and quickly.
What is an .htaccess file
An .htaccess file is a simple ASCII file that you would create with a text editor like Notepad. It provides a way to make configuration changes on a per-directory basis.
Note that .htaccess is the file’s extension. It isn’t file.htaccess, it is simply .htaccess.
.htaccess files affect the directory they are placed in and all sub-directories. For example if there is an .htaccess file located in your root directory (yoursite.com) would affect yoursite.com/content, yoursite.com/content/contents, etc. It is important to note that this can be prevented (if, for example, you did not want certain htaccess commands to affect a specific directory) by placing a new htaccess file within the directory you don’t want affected with certain changes, and removing the specific command(s) from the new htaccess file that you do not want affecting this directory.
Features
With an .htaccess file you can:
- Redirect user to different page
- Password protect a specific directory
- Block users by IP
- Preventing hot linking of your images
- Rewrite URI’s
- Specify your own Error Documents
In this tutorial we’ll only be focusing on rewriting URI’s.
Removing Extensions
To remove the .php extension from a php file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to alter the code above to match the filename:
RewriteRule ^([^\.]+)$ $1.html [NC,L]
That’s it! You can now link pages without needing to add the extension of the page. For example:
<a href="/wallpaper" title="wallpaper">wallpaper</a>
Conclusion
For those who are not so experienced with .htaccess files there is an online tool for creating .htaccess files. It is pretty good for novice users and very easy to use. Visit the website.
If you have any questions, you can add them in the comments section below.
References:
Comprehensive guide to .htaccess (javascriptkit.com)
Source Files are unavailable for this tutorial! No Source Files








Wow! I had no idea that the HTML and PHP extensions could be removed this way. This would have come in handy when I used to build sites using Dreamweaver as the publisher. This might still come in handy in the future. Do you think this might hurt search engine indexing at all? Thanks!
@Adam: That’s a very good question Adam! As far as I know, short URL’s are better for click-through rates and it makes it easier for the user to remember it. If you’d change your page from /yourpage.html to /yourpage you’d also redirect (permanent redirect, 301) the old page to the new one, thus notifying the spider crawler that the page has moved, preserving your search engine rankings! So, no, I don’t thing it would hurt indexing at all!
Hope that answered your question!
Very nice idea, but I’m having a problem. After inserting this code in htaccess, when I click to a php file, I’m redirected to home page.
Any idea of what am I doing wrong?
Thank you and congratulations for your blog!
@tradiArt: What code are you inserting? Make sure it’s the:
RewriteRule ^([^\.]+)$ $1.php [NC,L]Then how are you linking the pages? you should be able to link them like that (omitting the .php extension):
< a href="/wallpaper" title="wallpaper">wallpaper< /a>Also where do you have the htaccess file? is it in the root directory? Do you have anything else in the htaccess file?
I tried this but it would be nice if you can still link to the page.php and just have the htaccess file remove it for you (visually). This is good if you’re using files on different sites and things like that.
Wow, I can’t thank you enough, after all the converters online to change “/something.php” to just “/something” – this is the FIRST thing that worked for me.
It’s weird how every website that converts these extensions gives you different output.
I’m a designer and I didn’t get it. Thank you so much!
Hi Alex, I have been trying to get a .htaccess file to work for a couple days and I’m completely frustrated. Your blog seems very clear and concise so I think scripting is just not my thing. I set up a test subdirectory with the .htaccess file. Do you have any ideas what I’m doing wrong? http://www.thinked.com/test/
I tried to vary the hrefs to see if that made a diff. Thanks very much for any advice you could offer.
@Robbie: You’re welcome
@panamond: What do you want it to do? I see that the extension is removed from the links!
Hi Alex. For example, I thought the href link http://www.thinked.com/test/page2 should go to http://www.thinked.com/test/page2.html, but it doesn’t. I’m sorry if I don’t understand this.
@panamond: Well do you have a different .htaccess in the test folder? If yes, edit that one. Make sure that the name file of the htaccess is “.htaccess”. Next add this code:
Options +FollowSymLinksOptions +Indexes
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Finally, to link it add
a href="page1"without the forward slash at the end (/)! It should work!Hi everyone,
Thanks for the post and info. I have the rewrite rules working on my site (removed .html)
Is it possible to make the htaccess automatically redirect the domain.com/test.html to domain.com/test
Right now I can access the site on both, can I redirect the .html url to the non-.html url?
Thanks!
@Crece: I couldn’t get it to redirect the page in the normal way. I don’t think you can do that!
If it was so, all this fuss wouldn’t be required. You would just redirect the page to the one without the extension!
One thing you could do is remove the extension manually and if someone enters the file with the extension, they’ll get a 404!
But I don’t think that’s a better solution!
@Alex: Thanks! Just checked out your portfolio. Props there.
True, makes sense. But for example I have this in my htaccess now:
# index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
It basically “forces” the url go to the non-”index.html” url (domain.com/) even if I type it down with the index.html.
I read a couple of other articles and I think the key here is the [R] tag that stand for: Redirect and “instructs Apache to issue a redirect, causing the browser to request the rewritten/modified URL.” (source: http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/)
I tried applying that tag to other rewrite rules but didn’t seem to work, though.
I think it can be done. For example some sites force a trailing slash to the url, so the user doesn’t get a 404 error.
What do you think? I have been tinkering with this all morning!
@Crece: I tried but I couldn’t get anywhere with it! Look at how WordPress does it, it might be helpful.
For example if you go to http://www.sicanstudios.com/pages/blog/index.php it redirects it to http://www.sicanstudios.com/pages/blog/
This is what’s in the .htaccess file:
RewriteEngine On
RewriteBase /pages/blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /pages/blog/index.php [L]
Hey this is very nice! i have some problem i link to both php and html… html extentions is working fine but the php thing broke the php contact form why is that??? is it possible to fix…
Gr,
patrick
@patrick: you probably have to your contact form a code saying:
form id="contact_form" action="FormToEmail.php" method="post"
right? Well the file which it points to (in this case “FormToEmail.php”), is “not there” anymore since the “.php” is removed by the server. Try renaming the action file without the “.php” extension
Hey Alex,
Just wanted to say thanks for such an easy to understand tutorial. Works perfectly.
I might visit this blog often now
@Josh: glad it was helpful!
Hi Alex. I’m having errors, I’ve changed “mysite.net/community.php” to “mysite.net/community” But now pages like… “mysite.net/community/staff” Don’t work.
I don’t have a folder called community, by the way, I just got 1 .htaccess folder, it works on XAMPP but not webhosts. help?
*EDIT*
Fixed.
@Brooke: Glad to hear that! If you want, I invite you to share what was the problem and how you fixed it so that others can benefit from it
Trackbacks