Wednesday, 10 February 2016

Make SEO Friendly / Clean Url in PHP using .htaccess



In this tutorial I will show you how to make seo friendly or clean url in PHP programming language using apache .htaccess file. Semantic Url is one of the required things for Content Marketing. With the help of pretty url our website content will be Indexed on different search engine very fast. Clean Url is one part of Search Engine Optimization. SEO Friendly Url  will increase our ranking on different search engine. With the help of this clean slug our presence on search engine will increase. So, How to gain this feature for our ecommerce website, these all things are learn in this video. I think you will learn this things from this video.



Table Structure


 CREATE TABLE IF NOT EXISTS `tbl_post` (  
  `post_id` int(11) NOT NULL AUTO_INCREMENT,  
  `post_title` text NOT NULL,  
  `post_text` text NOT NULL,  
  `post_url` text NOT NULL,  
  PRIMARY KEY (`post_id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;  

index.php


 <?php  
 $connect = mysqli_connect("localhost", "root", "", "test_db");  
 if(isset($_POST["submit_btn"]))  
 {  
      $post_title = mysqli_real_escape_string($connect, $_POST["post_title"]);  
      $post_text = mysqli_real_escape_string($connect, $_POST["post_text"]);  
      $post_title = htmlentities($post_title);  
      $post_text = htmlentities($post_text);  
      $sql = "INSERT INTO tbl_post (post_title, post_text, post_url) VALUES ('".$post_title."', '".$post_text."', '".php_slug($post_title)."')";  
      if(mysqli_query($connect, $sql))  
      {  
           header("location:post/".php_slug($post_title)."");  
      }  
 }  
 function php_slug($string)  
 {  
      $slug = preg_replace('/[^a-z0-9-]+/', '-', trim(strtolower($string)));  
      return $slug;  
 }  
 ?>  
 <html>  
      <head>  
           <title>Make SEO Friendly / Clean Url in PHP using .htaccess</title>  
           <style>  
           .container  
           {  
                width:700px;  
                margin:0 auto;  
                border:1px solid #ccc;  
                padding:16px;  
           }  
           .form_text  
           {  
                width:100%;  
                padding:6px;  
           }  
           </style>  
      </head>  
      <body>  
           <div class="container">  
                <h3 align="center">Make SEO Friendly / Clean Url in PHP using .htaccess</h3>  
                <form name="submit_form" method="post">  
                     <p>Post Title  
                     <input type="text" name="post_title" class="form_text" maxlength="200" />  
                     </p>  
                     <p>Post Text  
                     <textarea name="post_text" class="form_text" rows="10"></textarea>  
                     </p>  
                     <p><input type="submit" name="submit_btn" value="Submit" />  
                </form>  
           </div>  
      </body>  
 </html>  

post.php


 <?php  
 $connect = mysqli_connect("localhost", "root", "", "test_db");  
 $post_url = $_GET["post_url"];  
 $sql = "SELECT * FROM tbl_post WHERE post_url = '".$post_url."'";  
 $result = mysqli_query($connect, $sql);  
 ?>  
 <html>  
      <head>  
           <title>Make SEO Friendly / Clean Url in PHP using .htaccess</title>  
           <style>  
           .container  
           {  
                width:700px;  
                margin:0 auto;  
                border:1px solid #ccc;  
                padding:16px;  
           }  
           .form_text  
           {  
                width:100%;  
                padding:6px;  
           }  
           </style>  
      </head>  
      <body>  
           <div class="container">  
                <h3 align="center">Make SEO Friendly / Clean Url in PHP using .htaccess</h3>  
                <?php  
                if(mysqli_num_rows($result) > 0)  
                {  
                     while($row = mysqli_fetch_array($result))  
                     {  
                          echo '<h3>'.$row["post_title"].'</h3>';  
                          echo '<p>'.$row["post_text"].'</p>';  
                     }  
                }  
                else  
                {  
                     echo '404 Page';  
                }  
                ?>  
           </div>  
      </body>  
 </html>  

.htaccess File


 RewriteEngine On  
 RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?post_url=$1  
 RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?post_url=$1  

16 comments:

  1. Hello webslesson

    I am try this code. data not show in my webpage.
    i split this code in three step.
    1. New update
    2. View all post
    3. show details

    http://www.searchanzsco.com/news-update.php
    http://www.searchanzsco.com/australia-immigration.php then click on Title

    data not show on page.

    ReplyDelete
  2. how to place keyword in php

    ReplyDelete
  3. is only single url but cant work if use multi id
    blog?id=1&pic?id=1

    ReplyDelete
  4. When i am using .htaccess the css file is not loading... What to do?

    ReplyDelete
  5. if two posts have same name then.....
    ---------------------------
    for example
    post 1 name is: Hello-Word
    post 2 name is: Hello-Word
    --------------------------
    Then what we do....?
    Tell me plz....

    ReplyDelete
  6. plz...... reply my comment

    ReplyDelete
  7. Hello friends in this post we are going to learn how to use Datatables jQuery plugin with php mysql and how can we use this plugin with Bootstrap… Read More

    ReplyDelete
  8. Please we lesson, I have really enjoyed your coding style. Pls I want you to make a tutorial on how to use mpdf to create PDF document in php. Please help us

    ReplyDelete
  9. you guys are doing a great job but try to respond to question from your viewer in the comment section of you youtube page and here too

    ReplyDelete
  10. my question is, if i have a page with products displayed in a table with links for delete, edit, viewproduct like(example: viewproduct.php?id=$7), how will i set the url to be seo friendly.
    because i don't want people to see the server side id values on clicking the viewproduct or edit product link?.
    thanks waiting for your kind reply

    ReplyDelete
  11. How to use the same thing for another url

    for example: post.php and post2.php

    how to define in .htaccess

    ReplyDelete
  12. Best tutorial ever for web developer

    ReplyDelete