0

I have made a blog in PHP, on Post.php with If isset get I am checking Post ID and than getting slug of same ID from database and accessing the post with that slug.

The issue is its generating Url like: Post.php?Slug=postname-title I want to make it Post.php?postname-title OR urls.com/postname-title

Please note I am getting post from database matching with slug If any how I do not set php to Get in url it is not getting value from the same slug.

Please guide if it will be done via .htaccess or any other way?

 <?php
            // add mysql_real_escape_string to slug, to prevent sql injection
            $slug = $_GET["slug"];
            $sql = "SELECT * FROM posts WHERE slug = '" . mysqli_real_escape_string($db, $_GET["slug"]) . "'";
            $result = mysqli_query($db, $sql);
            if (mysqli_num_rows($result) > 0) {
                // output data of each row
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "<h2>" . $row["title"] . "</h2>";
                    echo "<div id='post-details'>" . "Posted on " .  $row["post_date"] . "  --  " . "Last Update " .  $row["last_edited"]  .
                        "</div>";
                        
                        if (loggedin()) {  echo "<div id='postactions'>This Post:<a href='create_note.php?id=" . $row["id"] . "'>Add Notes</a>";
                            echo '<a href="delete_post.php?id=' . $row["id"] . '" onclick="return confirm(\'Are you sure you want to delete this post?\')">Delete Post</a>';
                            echo '<a href="edit_post.php?id=' . $row["id"] . '">Edit</a></div>'; } 

 
                    $cat_id = $row["cat_id"];
                    $sql = "SELECT * FROM categories WHERE id = $cat_id";
                    $resultone = mysqli_query($db, $sql);
                    if (mysqli_num_rows($resultone) > 0) {
                        // output data of each row
                        while ($rowone = mysqli_fetch_assoc($resultone)) {
                            $cat_name = $rowone["name"];
                            echo "<div id='post-category'>" . "Category: <a href='all-categories.php?cat_id=$cat_id'>$cat_name</a>" . "</div>";
                        }
                    }



            echo "<p>" . htmlspecialchars_decode($row["body"]) . "</p>";
          
            $_SESSION["post_id"] = $row["id"];
                    $postid =  $row["id"];
                }
            } else {
                echo "0 results";
            }

            ?>
  • Does this answer your question? [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – CBroe Jul 11 '22 at 14:14
  • I'll read in details and update here if this help, However I was looking for a quick solution to this. – Abbas Hassan Jul 11 '22 at 18:59

1 Answers1

0

If you want a simple .htaccess version, you could use the following rule: RewriteRule ^post/(.+) Post.php?Slug=$1

This will change your URL from a pretty URL like: yourdomain.com/post/postname-title to yourdomain.com/Post.php?Slug=postname-title

Kevin
  • 1,068
  • 5
  • 14
  • 16