How to make SEO friendly website in PHP

Screen showing PHP SEO

In this short article we will explain how you can make an SEO friendly website – or URL – in PHP, by using the .htaccess rewriting engine. Having a clean URL makes it easier for Google, Bing, and other search engines to discover that your web page exists, and it ranks your page higher in relation to similar pages.

We took a small chat with one of Norways leading SEO experts, André Mikalsen, the CEO of Mikalsen Utvikling, and his comment was that “Way too many bloggers, artists, and other entrepreneurs who want to use a website to get their message out, end up settling for a subpar URL, and therefore a website with subpar SEO.” He helped us develop this guide for you to follow, also he has a very indepth guide on https://mikalsenutvikling.no/sokemotoroptimalisering/.

To make an SEO friendly website, you begin by enabling the Apache re-write_module, which will make it possible for you to improve the SEO of your URL by just busting out some lines of code. We do advise against using codes like the ones below if you’re not familiar with coding your website manually. But if you are, go ahead and use it!

.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>

So there it is: the code to improve your website’s SEO. Hope it helps!

Related Post