Friday 23 December 2016

How to Create SEO Friendly URL in PHP with htaccess





In this post describe how to make Clean or Semantic or SEO Friendly URL with Mysql Table Dynamic Content by using .htaccess mod_rewrite in PHP Script. This post will explain how can we Rewrite Dynamic dirty url with id to Semantic URL in PHP with using .htaccess. Clean url will increase your web site presence on search engine like Google, yahoo, bing etc and it will improve ranking on Search Engines. If you want to make SEO Friendly or Clean or Semantic Url for your site, so first you want to enable Apache re-write_module at your wamp server. By enabling Apache rewrite_module you can make clean SEO friendly url from your dynamic dirty url by writing few lines of PHP code and .htaccess file. For Semantic url you want to create .htaccess file in your working folder.

Source Code


.htaccess


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

index.php


<?php
$connect = mysqli_connect("localhost", "root", "", "test_db");
if(isset($_POST["submit_btn"]))
{
 //mysqli_real_escape_string() - mysqli_real_escape_string() function escapes special characters in a string for use in an SQL statement
 //htmlentities() - htmlentities() function converts special characters to HTML entities.
 $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
//post.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>

2 comments:

  1. Hello sir,
    How to make download file/images from database in codeigniter.please provide these type of code.

    ReplyDelete