Web Development Blog

Twitter

19 Aug, 2007

Removing file extension via .htaccess

Web Development » Snippets, Tutorials » Removing file extension via .htaccess

Problem:
You have the following URLs for your website:
www.example.com/about-us.html
www.example.com/services.html
www.example.com/contact-us.html

However, you would like to hide file extensions from the end users, and allow them to access to the files using the following URLs:
www.example.com/about-us
www.example.com/services
www.example.com/contact-us

Solution:
The solution can be achieved by using Apache’s mod_rewrite. Create an .htaccess file in your website root directory with the following content.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^.]+)\.html$ $1 [L]
# Replace html with your file extension, eg: php, htm, asp

Correction

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: php, htm, asp

Many thanks to Binh Nguyen (Commenter #1) for the correction

To add a trailing slash at the end of the URL
(for example: http://www.example.com/about-us/ to go to http://www.example.com/about-us.html)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html 

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Benefits:
- Search engine friendly
- Easier to read and remember
- Extension/environment independent; when you change the technology used for your website (for eg: from using asp to php), you can be assured that all the links and bookmarks will still work.

Recommend Book

Technorati Tags: , , , ,

Other similiar posts that you might be interested in:

29 Responses to "Removing file extension via .htaccess"

1 | Binh Nguyen

September 26th, 2007 at 5:20 am

Avatar

ERRRH!!! Your code doesn’t work.

I got this error message:
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, support@bonaway.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

The real working code should be like this:
For PHP
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

2 | Martin

October 25th, 2008 at 1:47 am

Avatar

Binh Nguyen is correct, the code in this article will cause an internal error. However, his code is great! Worked a charm!

3 | Danny

November 3rd, 2008 at 1:35 pm

Avatar

The article’s code gives me an internal error but I’m getting page not found with Nguyen’s code

4 | Danny

November 3rd, 2008 at 1:42 pm

Avatar

Oops wrong page. It actually works. Now I just have to remove all the extensions from my links.

5 | Kevin

December 6th, 2008 at 4:50 am

Avatar

Do I have to get rid of the .php in my files, can’t it do it automatically?

6 | celsius

December 20th, 2008 at 4:41 am

Avatar

@kevin

yes, this is what the rewrite code is all about. the php files remain, but the browser gets a cleaner url. just make sure you update your links by dropping the extension, and you’ll be all set.

7 | Salwey

December 24th, 2008 at 11:53 am

Avatar

it works fine but how to prevent users from using .php extention if they type it manualy instead it shows error page?

8 | ROshan

December 30th, 2008 at 12:53 pm

Avatar

Thanks….let me try!

9 | CC

February 7th, 2009 at 8:43 am

Avatar

Is it possible to add an ending “/” to the URL so that it looks like a directory?

i.e., http://www.example.com/services/

The current code generates an error done like this.

10 | Pieter Christiaens

March 20th, 2009 at 11:05 pm

Avatar

Hello! I would also like to know how to make it with a “/” on the end.

I am changing a wordpress blog to a static site, so this would be really helpful!

11 | eisabai

March 26th, 2009 at 9:34 pm

Avatar

To add / at the end

Replace the last line of code:
RewriteRule ^(.*)$ $1.html

With these lines:
RewriteRule ^([^/]+)/$ $1.html

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

12 | Paul Nate

March 29th, 2009 at 9:36 pm

Avatar

I used this code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html

# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

and it would not work and if i remove html at end it will show me a page but it would not load CSS Help please?????

13 | eisabai

March 29th, 2009 at 10:26 pm

Avatar

Hi Paul, can you give me the URL of the page so that I can have a look for you? You should be able to fix the css issue by using an absolute path, rather than a relative one.

14 | axelator

April 5th, 2009 at 8:38 am

Avatar

hey, good thread & got the right answers. thanks everybody!

one question, how to deal with situation where two files have the same name but different extensions?

e.g., in the same directory:

foo.html
foo.php

the links would both be href=”foo” with this method, how does one deal with this? (assuming the file names must not change)

15 | Pieter Christiaens

April 6th, 2009 at 6:25 pm

Avatar

Hello!

Thanks for the tips! I am also having the same problem as Paul described. The css file is not loading and the images also not! Any more tips?

16 | Pieter Christiaens

April 6th, 2009 at 6:34 pm

Avatar

I tried doing it with the absolute path and it works. The images don’t work though!

Can this be done without changing the filepath of the css and the images?

The problem is that it gives an error when I put in the url with the / at the end. Can it redirect to the version without the slash?

17 | Michael Robbins

April 23rd, 2009 at 1:59 am

Avatar

Great Article really needed this

18 | a guy

May 29th, 2009 at 1:46 pm

Avatar

If you make all of your links absolute it works ( absolute url to your css files, image files etc )

19 | Grant

June 1st, 2009 at 2:53 am

Avatar

Hi

I have both .html and .php pages and would like to hide both extensions on the same site using one .htaccess file – is this a possibility, if so, it would be appreciated if you could make an update to this post.

Thanks

20 | Julia

June 21st, 2009 at 10:49 pm

Avatar

Hello Eisabai,

I digged a lot of sources looking for a good reference of this issues and I’m so glad to find your website.

Your article is the best. Thank you and thanks to all people who commented above and contributed to correcting and improving this code.

BTW, I had the same problem that some people mentioned before – with CSS and images. I solved it with ../ in my relative path.

21 | desiz

July 29th, 2009 at 10:40 pm

Avatar

Hi…

trailing slash function working but dnt show
images & css :(

plz mail me the correct …

22 | miva

August 22nd, 2009 at 12:00 am

Avatar

Will this code work for .shtml extension?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.shtml -f
RewriteRule ^(.*)$ $1.shtml

23 | eisabai

August 24th, 2009 at 11:07 am

Avatar

Hi miva,

Yes, I believe so.

24 | Tutorial City

October 21st, 2009 at 9:56 pm

Avatar

Would this work for php,html,css,jpg,gif,png?

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.(php|html|css|jpg|gif|png) -f
RewriteRule ^([^/]+)/$ $1.html

25 | Zeta

December 16th, 2009 at 5:29 am

Avatar

I have tried all of the above, it shows the 403 error(((

Forbidden
You don’t have permission to access /accessories/ on this server.

26 | Jen

January 25th, 2010 at 5:47 pm

Avatar

Code works a dream. Now I have /something and no longer /something.html

Next issue though ….. I do not want to set up 301 redirects for every single page, from the /something.html version to /something

Is there a single piece of code I can insert into the .htaccess file to solve this or am I facing having to set up 40+ 301 redirects?

Hope somone can advise,

jen

27 | Kevin

February 15th, 2010 at 7:33 am

Avatar

DUDE! I searched high and low for something like this and after a day of searching and trying to tweak it myself, I found this page.

You are a lifesaver.

THANK YOU!

28 | Peter

May 28th, 2010 at 8:43 pm

Avatar

Hey this is kewl and I can get it to work for files in the root dir but how can I get it to work for subdirectories?

29 | Nic

July 13th, 2010 at 4:09 am

Avatar

I’ ve got the same question as Peter, How do you make this work for sub domains, if you have http://www.dev.mysite.com/test.html –> http://www.dev.mysite.com/test/

Comment Form

ebook
Subscribe to our newsletter and receive FREE e-book "7 Days Exercise to Build More Traffic To Your Blog"
Your name:  
Your email:  

  • About
    The blog Web Development Blog is where I keep myself up to date with the latest technologies in the industry and share my ideas and thoug...
  • Adsense Tips and Tricks
    With personal and commercial blogs, community websites, and social pages becoming the hot topic of the Internet in the last few years, many...
  • Advertise With Us
    Web Development Blog is a blog about Web Development, Web Design, Web Applications, Web 2.0, AJAX, Search Engine Optimisation, Latest Techno...
  • Archive
    Grab yourself a cup of coffee or tea, sit back and browse through an archive of all the blog posts on Web Development Blog....
  • Choosing a Content Management System
    The most commonly asked question when it comes to Content Management System is whether to build or buy (pre-built). While there are many fa...
  • Coupons
    Use the following coupon codes, promos and discount codes to save money on web hosting, domain names, website templates, ebooks, software, p...
  • Customised WordPress Themes
    If you would like a customised WordPress theme for your blog or your website, read on: (or head to WordPress Themes page to get free WordPre...
  • Favourite Poems
    If by Rudyard Kipling If you can keep your head when all about you Are losing theirs and blaming it on you, If you can trust yourself w...
  • Favourites
    Books Web Development Books Magazines Glamour Reader's Digest Software Adobe Photoshop CS4 Trend Micro Antivirus + Antispyw...
  • How to Start a Blog?
    What's a blog? According to Wikipedia, a blog is a user-generated website where entries are made in journal style and displayed in a reve...
  • Introduction to JSP Standard Tag Library (JSTL Basics)
    Tag libraries to include in your .jsp page <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> &l...
  • Links
    Free Anonymous Proxy Increases your privacy and security on the Internet by using our free, fast and easy to use web proxy. data backu...
  • Making a Passive Income Online
    Passive income, in my opinion, means an income which does not require a lot of regular maintenance work or continuing effort. I have been t...
  • Online Distribution Channels
    One of the importances of any website or business is to bring your products or services to the right people and to reach the target audience...
  • Recent Projects
    Followings are some of the recent projects I have done in 2006 -- 2007. Check out my latest business venture Web design Sydney to get your...
  • Recommended Web Development Books
    Many developers often ask me what books I read and what books I recommend in regards to web development and web technologies such as AJAX, J...
  • Search
    Looking for something on Web Developement Blog? Use this search tool powered by Google Custom Search to find what you are looking for just ...
  • Seven Tips to Building an Online Presence for your Business
    1. Get a domain name for your business The first and foremost step in building an online presence is to secure a domain name for your busi...
  • Seven Tips to Increasing Your Website’s Traffic Using SEO
    SEO is the buzz word - many people have mentioned it and many have heard of it too, but very few people know how to implement it properly. ...
  • Subscribe
    Newsletter [newsletter] RSS 2.0 feeds Entries feed Comments feed Subscribe using your favourite web-based or desktop feed r...
  • Useful Linux Commands
    Find files older than 60 days find * -mtime +60 Delete files in backup folder which are older than 60 days rm -f `find /backup/ -mtime ...
  • Web Design Tips
    Nowadays, having a website for your business is like having a phone number. Almost every business has their own website, with their own doma...
  • Web Development Books
    Many developers often ask me what books I read and what books I recommend in regards to web development and web technologies such as AJAX, J...
  • Web Hosting Comparison Chart
    The following are some of the most popular web hosting providers. I have personally used the recommended ones. Hosting coupon codes will ...
  • Web Standards
    Web Standards is defined as a set of rules or specifications that should be followed when developing a website.  The main objectives o...
  • WordPress Themes
    All WordPress themes are designed and coded by esn studio, and licensed under GPL license. If you would like a customised WordPress theme fo...
  • Sam: Useful. I always forget the rel target one
  • Chronic Tinnitus: Hi, Thanks for taking the time to discuss a really confusing matter - I find Plesk so much more awkward than the cpanel that I'm used to. Thanks Chr
  • Serhiy: I personally liked the web hosting comparison chart. Especially the recommended *. I got Bluehost :) .-= Serhiy´s last blog ..

Interests

Web development, Web design, Open source technologies, Portal development, APIs, Web services, Social media applications, Search engine optimisation, Mobile application development, iPhone Apps, Web 2.0, Web 3.0, Latest Internet technologies

Misc.

  • bluehost Hosting $6.95/month
  • Joomla Templates