.htaccess 如何从我的帖子中删除Slug,Post.php?Slug=postname-title

zqdjd7g9  于 2022-11-25  发布在  PHP
关注(0)|答案(1)|浏览(97)

我已经在PHP中做了一个博客,在Post.php上,如果isset get我正在检查帖子ID,然后从数据库中获取相同ID的鼻涕虫,并使用该鼻涕虫访问帖子。
问题在于其生成的URL如下所示:我想把它改成Post. php?postname-title或者urls.com/postname-title
请注意,我从数据库中获得与鼻涕虫匹配的帖子,如果有任何我不设置PHP来获取URL,它不会从相同的鼻涕虫中获得值。
请指导是否将通过.htaccess或任何其他方式完成?

<?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";
            }

            ?>
mftmpeh8

mftmpeh81#

如果您需要简单的.htaccess版本,可以使用以下规则:RewriteRule ^post/(.+) Post.php?Slug=$1
这会将您的URL从一个漂亮的URL更改为:yourdomain.com/post/postname-titleyourdomain.com/Post.php?Slug=postname-title

相关问题