SEO – URL using php code and .htaccess

In this post i would like to explain about generate seo urls using php code and .htaceess. The following function for seo urls generation with the php code

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 .

.htacess file

<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…

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

7 Responses

  1. Srinivas says:

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

  2. jhon says:

    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. Hello ,

    the following will help for you,

    Options +FollowSymLinks

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

    once try with this.

  4. Mitendra says:

    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 says:

    thanks now it’s working

  6. vijay says:

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

    http://www.mysite.com/index.php?page=aW5kZXhib2R5

    to

    http://www.domain.com/index.php

    anybody can coach me how to make this one..

  1. November 8, 2011

    […] that website php code is written in a static htm file. For that we just put a one line code in a .htaccess file, it will work. 1AddHandler application/x-httpd-php .html […]

Leave a Reply

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