In this post, I would like to explain how to generate SEO-friendly URLs using PHP code and .htaccess. The following function is used for SEO URL generation in the PHP code.

The world of Search Engine Optimization (SEO) is constantly evolving, and one key element of SEO success is the structure of your website’s URLs. SEO-friendly URLs not only make it easier for search engines to index your content but also enhance the overall user experience. In this article, we’ll delve into the art of creating SEO-friendly URLs using a combination of PHP code and the powerful .htaccess file.

For many web developers and site owners, the term “SEO-friendly URL” might seem complex and daunting. However, it’s an essential aspect of on-page SEO that can significantly boost your website’s visibility in search engine results pages. With the right knowledge and tools, you can harness the potential of PHP and .htaccess to optimize your website’s URLs for both search engines and human visitors.

PHP Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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

1
2
3
4
5
6
7
8
<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]

In conclusion, mastering the creation of SEO-friendly URLs using PHP code and .htaccess is a vital step towards improving your website’s SEO and overall user experience. By crafting clean, concise, and descriptive URLs, you make it easier for search engines to understand and rank your content. Additionally, you create a more user-friendly browsing experience, helping visitors navigate your site effortlessly.

As the digital landscape continues to evolve, optimizing your website for SEO becomes increasingly crucial. Your URLs play a pivotal role in this endeavor, and by following the techniques outlined in this article, you’re well on your way to ensuring that your website’s URLs are not only search engine-friendly but also user-friendly. Embrace the power of PHP and .htaccess to enhance your website’s SEO and provide a more satisfying online journey for your visitors.

Categories: PHP CodeSEO

7 Comments

Srinivas · January 28, 2010 at 9:49 am

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

jhon · February 8, 2010 at 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]

Anil Kumar Panigrahi · February 18, 2010 at 3:06 am

Hello,

the following will help you,

Options +FollowSymLinks

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

once try with this.

Mitendra · August 10, 2010 at 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

Mitendra · August 12, 2010 at 8:56 am

thanks now it’s working

vijay · September 7, 2010 at 1:31 pm

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

Few things about PHP - Anil Labs · November 8, 2011 at 9:03 pm

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

Avatar placeholder

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