Building Search Engine Friendly URLs with htaccess and PHP

Search Engine Friendly URLs

Search Engine Friendly URLs

This is tutorial about building search engine friendly URLs with htaccess and PHP  . URLs play a very important role in on page search engine optimization.Search engines rank static URLs(http://example.com/my-title.html) better than dynamic URLs(http://example.com/post.php?id=34).All dynamic URLs can be converted in static URLs easily with the help of “.htaccess” and PHP.We will try to understand these URLs rewriting techniques with the help of few examples.

1.In first example let us consider we have  a website which contains URLs like

http://example.com/post.php?id=1

http://example.com/post.php?id=2

http://example.com/post.php?id=3

Our purpose is to change these URLs in following formats.

http://example.com/my-first-post-title-1.html

http://example.com/my-second-post-title-2.html

http://example.com/my-third-post-title-3.html

An easy approach is to add the following rules in your .htaccess file.

Above rewrite rule with pass all request where URLs end with .html and contain  numbers before .html to your page post .php.In above code PT (pass through) flag used to pass request to “post.php”  page ,L(last) flag will stop matching other rules after this line and QSA(query string added) flag will pass query string “id” to page “post.php”.I have “id=$2″ because Apache assign unknown values of URLs to variable like $1,$2,$3 automatically on first come first serve basis.(.+) comes first in the rule so its values is assign id in $1 and ([0-9]+) comes after it so its value is assigned in $2.

2.In second example let us consider we have  URLs which contains more than one parameters like this.

http://example.com/post.php?cat=3&id=1

http://example.com/post.php?cat=2&id=2

http://example.com/post.php?cat=1&id=3

Our purpose is to change these URLs in following formats.

http://example.com/third-category-3/my-post-title-1.html

http://example.com/second-category-2/my-post-title-2.html

http://example.com/first-category-1/my-post-title-13.html

We can use following rules to .htaccess.

In above both examples URLs contains one or more numbers.If you do not want to display these numbers in URLs you can read example 3 and 4 ,but in these situations you have to change your PHP code slightly.

3.In third example let us consider we have  URLs like this.

http://example.com/post.php?id=1

http://example.com/post.php?id=2

http://example.com/post.php?id=3

Our purpose is to change these URLs in following formats.

http://example.com/my-first-topic-title.html

http://example.com/my-second-topic-title.html

http://example.com/my-third-topic-title.html

The difference between first and third example is  this we are not showing any identifier in URLs.

We can use the following rewrite rule for this.

Remember that the above rule will not pass “id” parameter to “post.php” page.Instead of “id” it will pass “title” to page “post.php”.We can retrieve the value of ID by adding the  following code to your page “post.php” at beginning.

 4.In fourth example let us consider we have  URLs which contains more than one parameters like this.

http://example.com/post.php?cat=3&id=1

http://example.com/post.php?cat=2&id=2

http://example.com/post.php?cat=1&id=3

Our purpose is to change these URLs in following formats.

http://example.com/first-category/my-first-post-title.html

http://example.com/second-category/my-second-post-title.html

http://example.com/third-category/my-third-post-title-.html

We can use following Rewrite rules to .htaccess.

Like third example here we can retrieve the paramers “catid” and “id” using following PHP code.

 

  • vovan

    can you show sql schemas too ?

  • guest

    how to display special characters like arabic,russian lenguage ?

    • http://megarush.net/ Amit Singh

      In order to use non-english keywords on your URLs, your database must be converted to UTF-8.Otherwise there is no problem to use non english keywords in urls.