Create Search Engine Friendly URLs using .htaccess

In this post, we will see how to use .htaccess to create search engine friendly URLs. Using .htaccess, mapping dynamic pages to static address is easy and this helps search engines to index the pages fast, effective and strong.

Initially, create a file .htaccess in your favorite editor, use the rewrite rule as below to map dynamic id passing url’s to static version. Use the code and create your dynamic pages for search engines spiders

**************************************************************
RewriteEngine on

## Rewrite Rules ##

RewriteRule ^category/([0-9]*)/([0-9]*)/([a-z_]*)$ category.php?id=$1&page_id=$2 [L]
RewriteRule ^category/([^/]*)/([a-z_]*)/([^/]*)$ category.php?r_name=$2&id1=$1

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

For eg: Originally the page URL looks like subcategory.php?id=5&page_id=1

Using the above .htaccess code, you can create the URL as

category/5/1/product_name

Here,

RewriteRule ^category/([0-9]*)/([0-9]*)/([a-z_]*)$

^ – Refer to start
Category – creates a folder as mentioned name
/ – Used for virtual partition
[0-9] – Writes single number as folder name
[0-9]* – Writes n numbers as folder name
[a-z_] – Writes single letters as folder name
[a-z_]* – Writes letters as folder name
$ – Refer to end

Additional information:

Your id passing pages might have indexed through some referral and so, you can use the below code to avoid error pages, create a 404pagenotfound.php and add your a message to your visitors to navigate to home page for information’s

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

## Trace Errors
ErrorDocument 404 /404pagenotfound.php

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

Hope you will like the post and its useful 🙂

Orignal Author : Jagadeesh M