Salt Lake City, Utah | USA
801.265.1100


That's right we know you love our content and we will be adding an expansion of blogs and discussions in the next month so stay tuned.



Full Blog Entry

Using .htaccess rewrite rules to hide .php extensions

March 5th 2010 10:10:21 | Tutorials
blog image for Using .htaccess rewrite rules to hide .php extensions

This post will cover a couple key features and what code we used to dynamically change our .php extensions to read .html while keeping the same filename so there is no need to rename files. This is very easy process and takes a couple easy to follow steps, we'll start out with some of the benefits of using this mod and then give you a breakdown of server settings you will want to check before you get started.

Benefits of rewriting your URL

Plain and simple, easier to read, the search engines will like your pages better and you can hide what language your website it coded in. A good example is when a search engine visits the dynamic url like product.php?id=5 it does not give much importance to that URL as search engine sees “?” sign treat it as a url which keeps on changing. so we’re converting the dynamic URL like the product.php?id=5 to static url format like product-5.html.

Required for URL rewriting

  • Check to see if .htaccess is supported, 95% of the people out there are on shared hosting and have access to .htaccess file system settings.
  • Check if your server host supports mod_rewrite. You can check this by creating a file called php_info.php and adding this line of code to the file <? php_info(); ?> then point your browser to the file to see your server settings.
  • FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error but most servers support all of the above by default.

Directions and examples

After you have checked to make sure your server supports all the the requirements you will need to create a file called .htaccess in the root of your website, this can easily be done on the ftp side of the site. If you use windows you might have an issue creating a .htaccess file due to the fact that microsoft hates linux and knows you're trying to create a file of the sort. Creating it server side via ftp then downloading the file to your desktop might be the best route if you are having issues creating the file. After you create the file add the following code to your .htaccess file:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*).html$ $1.php [nc]

The above example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.html is called in address bar it calls the file test.php. As you can see the regular expression in the first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+).html$ products.php?id=$1

The above example will rewrite the product.php?id=5 to product-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.



Comments:

 

1 2

WangS on March 25th 2010 23:51:29

Good fill someone in on and this post helped me alot in my college assignment. Thanks you as your information.
reply

Mr T. on March 30th 2010 09:17:59

Very well written blog i enjoy reading this site!
reply

Kyle S. on April 9th 2010 02:15:24

That is nice to definitely find a site where the blogger knows what they are talking about.
reply

School of Rock on April 11th 2010 03:47:18

Amiable brief and this post helped me alot in my college assignement. Gratefulness you for your information.
reply

Hermes on May 10th 2010 11:25:30

A good article Thank you!
reply

Forex on May 12th 2010 15:58:28

Really nice and impressive blog i found today.
reply

armani on May 21st 2010 18:18:46

nice to be here.... thanks for share
reply

Sunny on May 21st 2010 21:47:56

I am the first time on this site and am really enthusiastic about and so many good articles. I think it's just very good.
reply

burberry on May 24th 2010 04:14:48

Thank you for your great

content.
reply

Carter on May 24th 2010 18:50:49

Thank you for useful info. :-)
reply

 

1 2

 

You are sending reply to a comment. cancel?

Name (required)
Email (required - never shown publicly)
Your Comment

captcha

Categories


Blog Portal Links


Contact Us

We Are Available 24/7

Our main place of business and mailing address:

  • Duranged Design Inc
  • 574 W. Billinis Road
  • Salt Lake City, Utah 84115 USA
  • 801.265.1100