重写我的 htaccess 后,Php PDO 和 Get 方法无法在干净的 URL 中工作

标签 php css .htaccess

请问我的 PHP PDO、MOD-REWRITE 和 GET 方法有问题。 我重写了我的网站以使用干净的 url,并使用 php pdo 阅读发布的文章旧链接如下所示

http://example.com/questions.php?postid=145

重写后是这样的

http://example.com/questions/postid/145

但我的问题是,当我尝试使用干净的 url 阅读文章时,它不会很好地加载或显示 页面没有正确重定向 但大多数情况下它会在没有 css 的情况下加载并且所有图像都会不显示,而正常链接显示页面,因为它假设所有图像和 css 完好无损。请问我不知道是什么原因或如何解决它,有人可以帮助我吗?

请参阅下面的 .htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} ^[A-Z]{3,}\s/+questions\.php\?postid=([^\s&]+) [NC]
RewriteRule ^ questions/postid/%1? [R=301,L]
RewriteRule ^questions/postid/([^/]+)/?$ questions.php?postid=$1&$2& [L,QSA]

这是我的 PHP PDO

    <?php 
    session_start(); 
    include('dbconn.php');
    if(isset($_GET['postid'])){

    $db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
    $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $FINDID = $_GET['postid'];
    try{
    $find_stmt = $db_conn->prepare("SELECT * FROM blogs b 
    INNER JOIN users u
    ON b.UserName = u.username 
    WHERE b.IDS =:postid AND action = 'active'");
    $find_stmt->bindParam(':postid', $FINDID); 
    $find_stmt->execute();
    $FoundResault = $find_stmt->fetch(PDO::FETCH_OBJ);
    if ($FoundResault) {
    $IDS  = $FoundResault->IDS;
    $blog_title  = $FoundResault->blog_title;
    $blog_body  = $FoundResault->blog_body;
    $comments  = $FoundResault->comments;
    }
    $checkreply = $replies;

    }catch(PDOException $e){ echo "Error:" . $e->getMessage();}

    $db_conn = null;
    }   
    else{
    //echo "<meta http-equiv=\"refresh\" content=\"0;URL=".slash."index.php\">";
    }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body>
  <?php
  echo $blog_body."<br/>";
    echo $blog_body."<br/>";
    echo $comments."<br/>";
    ?>
  </body>
</html>

这是页面加载的方式

enter image description here

最佳答案

您的问题与 PDO 或 PHP 无关。 您的问题与 HTML 文档中的相对 URL 以及 CSS/JavaScript 文件等资源的工作方式有关。

让我们假设 URL http://example.com/question.php?postid=1234给出以下输出:

<html>
<head>
    <style rel="style.css">
</head>
<body>
    Question 1234: Text here
</body>
</html>

您的浏览器现在计算 style.css 的 URL 是相对于 question.php 文件的目录,它指向 http://example.com/style.css .

如果您现在将页面 URL 重写为 http://example.com/questions/1234 , 浏览器计算 css 文件的 URL 相对于 http://example.com/questions ,因为它认为这是页面 1234 的目录,而 css 文件的 URL 现在是 http://example.com/question/style.css .

最简单的解决方案是在同一目录中重写:将 question.php?postid=1234 重写为 question-1234:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^question-([0-9]+)$ /question.php?postid=$1 [L,QSA]

第二个最佳解决方案是将所有 CSS 和 JavaScript 文件的路径设置为绝对路径(以/开头),以便您的浏览器始终知道它们位于哪个目录中。如果您从 CSS 文件加载图像,则还必须使图像的绝对路径。

关于重写我的 htaccess 后,Php PDO 和 Get 方法无法在干净的 URL 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646238/

相关文章:

JavaScript 列表 : Delete and Edit items function?

html - 将 JPG 转换为 HTML

php - 重复输入mysql唯一字段

php - WordPress 表前缀不能为空

jquery - CSS/jQuery 库来近似 Google News UI 的外观和感觉?

.htaccess - 临时重定向到维护页面

php - 如何使用 htaccess 阻止垃圾邮件和垃圾邮件机器人?

php - POST 西里尔字母 PHP 产生特殊字符

php - 根据之前的选择选择字段

PHP SQL : How to display Unique ID to which data is saved just now?