Wednesday, August 23, 2017
Home » Social Media » SEO friendly urls using .htaccess and php

SEO friendly urls using .htaccess and php

Everyone wants to rank their site on the first page of search engine results. There are many factors which influence search engine rankings, and one of them is human readable URLs or SEO friendly URLs. A SEO friendly URL must reflect the content of the site or blog.

Let us suppose you are running a blog/website which is developed using custom PHP code. So, whenever you insert a new post on your blog, then the URL will be generated like this:

www.yoursite.com/index.php?blog_id=1234

In this tutorial, we will be changing the above URL to this:

www.yoursite.com/my-seo-url/

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]

index.php

$page_url = explode('-',$_GET['p'],2);
        if($page_url[1]!='')
        {
                $mod= $page_url[0];
                if($page_url[2]!='')
                echo $page= $page_url[1];
                else
                $page= $page_url[1];
        }
        else
        {
                $mod= '';
                $page= $page_url[0];
        }

include('header.php');
        if($mod!='')
        {
        include('pages/'.$mod.'/'.$page.'.php');
        }
        else
        {
        include('pages/'.$page.'.php');
        }
include('footer.php');

Example 1

here we explode – from first.
$url = ‘www.example.com/users-add’;
In above URL module is users and page is add
we explain above url with example
$mod = ‘users’;
$page = ‘add.php’;
pages/users/add.php
Example 2

$url = ‘www.example.com/welcome’;
if only page exist then
pages/welcome.php