Rachel H Kay Blog

How To Remove File Extensions From Your Sites URL

Do you have a website with ugly URL’s? Do you want to remove those ugly file extensions from your website's URL? You do? Well then, this post will make your day.

My main website, this website, is a static website that uses a flat CMS called Pulse to run this blog. While I prefer to keep my site as a static site, and don’t intend to move it into WordPress, there is one thing that I have hated about it. Those ugly URL’s with the .html and the .php at the end. So, I decided to scour the web looking for a way to get rid of those file extensions. I came across a lot of posts and forums that had different ways of getting rid of file extensions in the URL. Unfortunately, none of them worked. I was very frustrated by this point, because I just moved my site to a new server, and I wanted to remove those file extensions for a cleaner URL. Then finally I found a post in the support center on Inmotion Hosting’s website that had exactly what I needed to get rid of those blasted file extensions. Now the original code in the blog post did not work, but when I scrolled through the comments I found a fix for that code that does work. I then updated that code to not only hide .php file extensions, but .html file extensions as well.

How To Remove File Extensions

For those of you who aren’t familiar with the .htaccess file, you can find it in the root of your sites server, called public_html. If you don’t have an .htaccess file you can create one in order to implement the code. The great use of the .htaccess is to override certain things on your website such as protecting your login page, if you run a WordPress site, and directing other traffic to a 404 page, denying certain IP addresses access to your site, and a lot of other mod_rewrite rules that I’m not going to get into here. I’m still learning mod_rewrite myself.

In order to get rid of those pesky file extensions, just add this code inside the .htaccess file:

        
Options +FollowSymLinks -MultiViews        
# Turn mod_rewrite on        
RewriteEngine On        
RewriteBase /                

# Remove file extension from URL        
## hide .php extension        
# To externally redirect /dir/foo.php to /dir/foo        
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]        
RewriteRule ^ %1 [R,L,NC]                

## To internally redirect /dir/foo to /dir/foo.php        
RewriteCond %{REQUEST_FILENAME}.php -f        
RewriteRule ^ %{REQUEST_URI}.php [L]                

## hide .html extension        
# To externally redirect /dir/foo.html to /dir/foo        
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]        
RewriteRule ^ %1 [R,L,NC]                

## To internally redirect /dir/foo to /dir/foo.html        
RewriteCond %{REQUEST_FILENAME}.html -f        
RewriteRule ^ %{REQUEST_URI}.html [L]    

If you already have an .htaccess file that is already populated with other code, just place this code at the top. If there is already code in your .htaccess file that has RewriteEngine On then place the Options +FollowSymLinks -MultiViews above RewriteEngine On, then place the rest of the code below RewriteEngine On at the top of your file.

It's that simple!

comments powered by Disqus

About Me

Hi there. I'm Rachel Kay, a Web Developer, Illustrator, & Designer, whose hobby is to be creative and artistic, while freelancing as a Web Developer building creative, modern websites.

Categories