Jump to content

How to get Seo friendly URL in search bar in php


jadprash

Recommended Posts

I have posted three files one is .htaccess, search.php and header.php. Inside header.php I have a form that redirects to search.php. Basically, I am trying to make SEO friendly URL my current URL displays as example.com/blog/search.php?keyword=a expected result should be example.com/blog/search/a and example.com/blog/search.php?keyword=a&page=2 expected result should be example.com/blog/search/a/page/2. I have tried changing the .htaccess file and location in search.php but it doesn't work any solution, please.

htaccess file

RewriteRule ^search/([^/.]+)/page/([^/.]+)?$ search.php?keyword=$1&pn=$2 [L]

header.php

<?php if (isset($_GET['keyword'])) { $keyword = $_GET['keyword']; } else { $keyword = ""; } ?> <form class="d-flex" method="GET" action="search.php"> <input type="search" placeholder="Search" value="<?php echo $keyword; ?>" name="keyword" maxlength="70" autocomplete="off" required class="form-control me-sm-2"> <button class="btn btn-secondary my-2 my-sm-0" type="submit"><i class="fa fa-search" aria-hidden="true"></i></button> </form>

search.php

<? session_start(); include 'header.php'; //include 'config.php'; //pagination $keyword = $_GET['keyword']; if (empty($keyword)) { header("location: index.php"); } else{header("location: search/".$keyword); } if (!isset($_GET['page'])) { $page = 1; } else { $page = $_GET['page']; } $limit = 2; $offset = ($page - 1) * $limit; //pagination ends $sql = "SELECT * FROM blog LEFT JOIN categories ON blog.category=categories.cat_id LEFT JOIN users ON blog.author_id=users.user_id WHERE blog_title LIKE :keyword or blog_body LIKE :keyword ORDER BY published_date DESC LIMIT $offset,$limit"; $stmt = $conn->prepare($sql); $stmt->bindValue(':keyword', '%' . $keyword . '%', PDO::PARAM_STR); $stmt->execute(); $res = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <div class="container mt-2"> <h3>Search result for:<span class="text-primary"><?php echo $keyword ?></span></h3> <div class="row"> <div class="col-lg-8"> <hr> <?php if ($res) { foreach ($res as $result) { ?> <div class="card shadow"> <div class="card-body d-flex blog_flex"> <div class="flex-part1"> <a href="../../blog/sp/<?php echo $result['blog_id']; ?>"> <img src="<?php echo base_url('admin/upload/').$result['blog_image']; ?>"> </a> </div> <div class="flex-grow-1 flex-part2"> <a href="single_post.php?id=<?php echo $result['blog_id']; ?>" id="title"><?php echo ucfirst($result['blog_title']); ?></a> <p> <a href="" id="body"> <?php echo strip_tags(substr($result['blog_body'], 0, 200)) . '...'; ?> </a> <span><br> <a href="../../../blog/sp/<?php echo $result['blog_id']; ?>" class="btn btn-sm btn-outline-primary">Continue Reading </a></span> </p> <ul> <li class="me-2"><a href=""> <span> <i class="fa fa-pencil-square-o" aria-hidden="true"></i></span><?php echo $result['username'] . '... '; ?></a> </li> <li class="me-2"> <a href=""> <span><i class="fa fa-calendar-o" aria-hidden="true"></i></span> <?php echo date("d-m-y", strtotime($result["published_date"])) . '... '; ?> </a> </li> <li> <a href="category.php?id=<?php echo $result['cat_id']; ?>" class="text-primary"> <span><i class="fa fa-tag" aria-hidden="true"></i></span> <?php echo $result['cat_name']; ?> </a> </li> </ul> </div> </div> </div> <?php } } else { echo "<h5 class='text-danger'>No record Found</h5>"; } ?> <!-- Pagination begins --> <?php $pagination = "SELECT * FROM blog WHERE blog_title LIKE :keyword or blog_body LIKE :keyword"; $run_q = $conn->prepare($pagination); $run_q->bindValue(':keyword', '%' . $keyword . '%', PDO::PARAM_STR); $run_q->execute(); $arr = $run_q->fetchAll(PDO::FETCH_ASSOC); $total_post = count($arr); $pages = ceil($total_post / $limit); if ($total_post > $limit) { ?> <ul class="pagination pt-2 pb-5"> <?php for ($i = 1; $i <= $pages; $i++) { ?> <li class="page-item <?php echo ($i == $page) ? $active = "active" : ""; ?>"> <a href="search.php?keyword=<?php echo $keyword ?>&page=<?php echo $i ?>" class="page-link"><?php echo $i ?></a> </li><?php } ?> </ul> <?php } ?> </div> <?php include 'sidebar.php'; ?> </div> </div> <?php include 'footer.php'; ?>

 

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.