Write SEO friendly URL

June 13th, 2012 by Laeeq | 1 comment

Here in this post we will learn technique to write SEO friendly URL by hiding .php extension with url rewriting using .htaccess. Using this technique you will see item.html in the address bar of the browser but the actual file name remains item.php and you don’t need to rename the file extension.

Why we re-write URL?

Actaully when search engine crawl the dynamic url like item.php?id=12, it does not give much importance to that url as search engine sees “?” sign and treat it as a url which keeps on changing. So here we will convert the dynamic url like item.php?id=12 to search engine friendly url like item-12.html. we will rewrite the dynamic url in such a way that it will display as item-12.html in browser’s address bar.

Requirment for URL rewriting

For url re-writing, You will have to load mod_rewrite module in apache server. you can also check mod_rewrite module load using phpinfo() php function.

Examples of url rewriting for seo friendly URL

For rewriting the URL, you will have create a .htaccess file in the root folder of your web directory. And have to put the following codes as your requirement.

  1. Options +FollowSymlinks
  2. RewriteEngine on
  3. RewriteRule ^(.*)\.htm$ $1.php [nc]

The above example will rewrite the item.php to item.html. when a URL like http://phpzag.com/item.htm is called in address bar it calls the file item.php. As you can see the regular expression in 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.

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

The above example will rewrite the item.php?id=15 to porduct-15.html. when the URL like http://phpzag.com/item-15.html is called in address bar the file item.php?id=5 will automatically.

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Wrapping Text using PHP
  • PHP 5.5 beta 4 is now available
  • Swapping Array Keys and Values
  • PHP Security
  • Working with Image Metadata in PHP
  • Converting Between Relative and Absolute File Path
  • Parsing File Paths with PHP
  • How to remove HTML Comments with PHP?
  • How to access .htaccess values?
  • Dealing With Common PHP Errors
  • Interview Questions and Answers for Freshers
  • Display numbers with ordinal suffix using PHP
  • Function to set COLLATION on database fields of Mysql
  • Read and write to remote files using PHP
  • Sharing PHP SESSION Variables between Multiple Subdomains
  • PHP 5.5.0 Alpha4 Development Preview Released
  • Check a string starts with http using PHP
  • Cross-Site Scripting Attacks (XSS)
  • PHP 5.4.11 and PHP 5.3.21 released!
  • PHP Memory Leak Issue
  •  

     

    1. June 20th, 2012 at 09:39 | #1

      nice tutorials

    1. No trackbacks yet.