Murray, 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

Chanel on May 25th 2010 11:18:59

Thanks for good information that comes out to

read.
reply

Dior on May 25th 2010 20:14:56

Great journey and experience!
reply

HIPower on August 12th 2010 18:15:21

Great article, i

hope can know much information About it!
reply

PKV on October 1st 2010 17:00:13

the valuable ideas u provided do help our team's research for my group, appreaciate that.

- Lucas
reply

nacyangenna on November 20th 2010 20:10:33

I just sent this post to a bunch of my friends as I agree with most of what you’re saying here and the way you’ve presented it is awesome.

reply

thetarcarie on November 28th 2010 17:09:56

I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.

reply

 

1 2

 

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
  • 4740 So. Riverside Dr.
  • Murray, Utah 84123 USA
  • 801.265.1100

Feedback Submit your ideas or ask a question

Feedback
Thank you! Message Sent!
ERROR: Message Not Sent!