jadprash Posted September 16, 2022 Share Posted September 16, 2022 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'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/315336-how-to-get-seo-friendly-url-in-search-bar-in-php/ Share on other sites More sharing options...
ginerjm Posted September 16, 2022 Share Posted September 16, 2022 Tried to read thru you search.php code but your formatting makes it impossible. Quote Link to comment https://forums.phpfreaks.com/topic/315336-how-to-get-seo-friendly-url-in-search-bar-in-php/#findComment-1600647 Share on other sites More sharing options...
requinix Posted September 16, 2022 Share Posted September 16, 2022 7 hours ago, jadprash said: Basically, I am trying to make SEO friendly URL Search engines stopped caring about URL structures years ago. As long as you have the appropriate words in there, like "search" and the search keywords, then it's fine. Quote Link to comment https://forums.phpfreaks.com/topic/315336-how-to-get-seo-friendly-url-in-search-bar-in-php/#findComment-1600650 Share on other sites More sharing options...
jadprash Posted September 26, 2022 Author Share Posted September 26, 2022 I have attach sear.php in text foemat search.txt Quote Link to comment https://forums.phpfreaks.com/topic/315336-how-to-get-seo-friendly-url-in-search-bar-in-php/#findComment-1601051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.