tutorial thumbnail

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:

.htaccess (Wikipedia)

.htaccess files (Apache.org)

Comprehensive guide to .htaccess (javascriptkit.com)

Source Files are unavailable for this tutorial! No Source Files

Have anything to say? Add your comment below!
Comments
  • [...] This post was mentioned on Twitter by Ekkalak Sitotai and RapidxHTML, soratofx. soratofx said: RT @instantshift Remove .php, .html, extensions with .htaccess http://ow.ly/15WoW8 < u_u [...]

    21 October 2009 at 11:11 pm
  • 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!

    22 October 2009 at 7:20 am
  • @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!

    22 October 2009 at 10:19 pm
  • 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!

    1 November 2009 at 4:37 pm
  • @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?

    1 November 2009 at 8:30 pm
  • [...] How to remove .php, .html, .htm extensions with .htaccess [...]

    5 December 2009 at 1:57 pm
  • 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.

    12 February 2010 at 9:23 pm