Home > PHP > PHP Code > SEO – URL using php code and .htaccess

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

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


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

.htacess file

<FilesMatch "andhra-foods">
ForceType application/x-httpd-php
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…

7 Responses

  1. Pingback : Few things about PHP - Anil Labs

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.