ANIL KUMAR PANIGRAHI 's Blog

SEO – URL using php code and .htaccess

Posted by: Anil Kumar Panigrahi in: ● April 9, 2009

The following function for seo urls generation with the php code

*************************************

<?php
function cleanURL($string)
{
$url = str_replace("'", '', $string);
$url = str_replace("%20", "", $url);
$url = preg_replace('~[^\\pL0-9_]+~u', '-', $url); 
// it is for substitutes anything but letters, numbers and ‘_’ with separator
$url = trim($url, "-");
$url = iconv("utf-8", "us-ascii//TRANSLIT", $url);  
// you may optimize for your own custom character map for encoding.
$url = strtolower($url);
$url = preg_replace('~[^-a-z0-9_]+~', '', $url); 
// This is for keep only letters, numbers, ‘_’ and separator
return $url;
}
 echo cleanURL("Anil's%20Blog%20For%20(PHP)");  
// Anil-Blog-for-PHP
?>

************************************

If we want to redirect the file like

***********************************

domain.com/ andhra-foods.php?cid=1 then that is direct to

domain.com/andhra-sweets-1

for that place the .htaccess file at the www domain folder .

*********************************

<FilesMatch "andhra-foods">
ForceType application/x-httpd-php
</FilesMatch>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^[^-]+-[^-]+-[^-]+-([0-9]+)$ andhra-foods.php?cid=$1 [L]
RewriteRule ^[^-]+-[^-]+-([0-9]+)$ andhra-foods.php?cid=$1 [L]
RewriteRule ^[^-]+-([0-9]+)$ andhra-foods.php?cid=$1 [L]

*******************************************

Thank you…

Share on Twitter

5 Comments to "SEO – URL using php code and .htaccess"

1 | Srinivas

28 de January de 2010 to ● 9:49 am

Nice tip.. Now your blog looking great. Include google adsence on wordpress

2 | jhon

8 de February de 2010 to ● 5:29 am

hi please help me.. How can I rewrite this URL

http://www.domain.com/index.php?article=2

to

http://www.domain.com/article/create-rounded-corners-with-css

anybody can coach me how to make this one..

I search on google using “alias” with .htaccess but I lose..

article=2 [is the ID of create-rounded-corners-with-css articles]

3 | Anil Kumar Panigrahi

18 de February de 2010 to ● 3:06 am

Hello ,

the following will help for you,

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^article/(.*)/$ index.php?article=$1

once try with this.

4 | Mitendra

10 de August de 2010 to ● 10:04 am

Hi anil m getting error like

Parse error: syntax error, unexpected T_STRING in C:\wamp\www\test\index.php on line 5

5 | Mitendra

12 de August de 2010 to ● 8:56 am

thanks now it’s working

Write Comment

About Me

  Anil Kumar Panigrahi
  Software Engineer (PHP)
  Blogger & Founder of Anil Labs
  Baruva, India

You can share with me . . .

My Own Contributions

1) First contribution for codeigniter framework ( multi language support using google translate API ). we can get files from http://github.com/nyros/codeigniter_multilanguage


2) Audio and Video Streaming using FFMpeg and PHP -complete doc file : http://www.docstoc.com/docs/8300349/Audio-and-Video-Streaming

Twitter Updates

Follow me @anil2u
  • We will always be thankful to our teacher for all the hard work and efforts they have put in, for educating us.- Happy teachers Day . . . :) - posted on 05/09/2010 05:39:13
  • Working on shipping cost functionality (UPS and USPS) for oscommerce shopping cart - posted on 01/09/2010 09:40:03
  • If your freedom hurts others, you are not free. You must not hurt others. - Vivekananda - posted on 31/08/2010 06:02:49
  • Another day starts with full of energy . Good Morning - posted on 31/08/2010 02:54:12
  • Nice explanation about php oops concept : http://youropensource.com/projects/182-OOPS-Concepts-in-PHP - posted on 30/08/2010 19:56:17