Removing .php and .html extensions from a url

So I have been making little upgrades to the site, and I came across a cool bit of code I wanted to share. In the web world, a good rule I try to follow is K.I.S.S and that stands for “Keep it simple, stupid.” People don’t need all the information they see on screen and this is very true with file extensions. Most people don’t care if they are looking at a .HTML or a .PHP page so why even show it to them as an added plus it gives you a little extra security to hide file extensions. I will, even though in something I already had in place, a code that will redirect traffic from yourdomain.com to www.yourdomain.com, it helps with SEO.

To do this you will need to change/create the .htaccess file, it normally sits at the root of your website. I will show this one step at a time, so you understand it.

We start with this line of code

Options +FollowSymLinks -MultiViews

It specifies whether or not apache should follow a simbolic link while looking for a file.
For example, if you have a file
/home/mydomain/www/myfile.html
which is a link to
/home/mydomain/somedir/therealfile.html
Then apache will only retrieve the realfile.html if you have that option enabled.

Next we have

RewriteEngine On

This turns on the rewrite functions that we will be using below.

Now we get to the first good part, the redirect.

rewritecond %{HTTP_HOST} ^yourdomain.com$
rewriterule ^ "http\:\/\/www\.yourdomain\.com\/" [R=301,L]

This part of the code will redirect browsers and adds the www. to the start.

First we need to set the root for our redirects.

RewriteBase /

The next part will cut off the .php from a page so you can go to www.yourdomain.com/about_us instead of www.yourdomain.com/about_us.php. This comes in two parts; one is to externally redirect, and the other is to internally redirect.

externally redirect Code:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

internally redirect code:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Now these two will only work for PHP, and we need something different for HTML pages. The Script for HTML pages is a little diffident, and you don’t need two kinds for internal and external.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

So lets put this all together:

# Enable turn on simbolic link following
Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On

# redirect from yourdomain.com to www.yourdomain.com
rewritecond %{HTTP_HOST} ^thefedoranerd.com$
rewriterule ^ "http\:\/\/www\.thefedoranerd\.com\/" [R=301,L]

RewriteBase /

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

# To internally redirectRewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

# hide .html extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]

Copy this code into note pad and then save it as .htaccess and under “save as type” select “all files”. Upload this to the root of your website and your good to go!

Just in case you wanted a clean copy you can download the code above already in .htaccess format below:
.htaccess file

Well i hope this is helpful to you guys, if it does not work for you let me know and if it helps i am running my hosting on a Apache server hosted with go daddy.

The Fedora Nerd – Ben Crawford

Related Posts

Oh Captain My Captain
The Christian Copy
Star Trek: A fight for equality
Nerds, Who Are We?

One thought on “Removing .php and .html extensions from a url

  1. I’m really inspired along with your writing talents and also with the format to your blog. Is this a paid subject matter or did you customize it yourself? Anyway keep up the nice quality writing, it is rare to look a great blog like this one today..

Leave a Reply

Your email address will not be published. Required fields are marked *