使用RewriteEngine时图片未加载?

时间:2017-02-01 11:51:31

标签: php html .htaccess url-rewriting

我创建了一个url shortener,我正在使用重写规则,但是当我打开它时,我的页面上的图像没有加载,这只是我第一次加载页面时的问题。如果我删除规则,加载页面,然后重新打开规则图像将保持不变,但是当我第一次打开浏览器并加载带有规则的页面时,图像将无法加载。任何人都可以帮我解决这个问题吗?这是我的代码。

的index.php

<?php
  include 'connect.php';

  if(isset($_GET['title'])) {
      $result = $conn->prepare("SELECT * FROM atomly_shortener WHERE id=?");
      $result->bind_param("s", $_GET['title']);
      $result->execute();

      $goto = $result->get_result()->fetch_array();
      $g = $goto[1];
      header("location: $g");
  }
  if(isset($_POST['submit_url'])) {
    if(substr($_POST['submit_url'], 0, 7) != "http://") {
        $longurl= "http://".$_POST['long_url'];
    } else {
        $longurl=$_POST['long_url'];
    }
    $sql = "INSERT INTO atomly_shortener (long_url) VALUES ('$longurl')";
    if ($conn->query($sql) === TRUE) {
    } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
    }
    $sql = "SELECT id, long_url FROM atomly_shortener WHERE long_url='$longurl'";
  $result = $conn->query($sql);
    if($result->num_rows > 0) {
      // Set session variables
      $result = $conn->query($sql);
      if($result->num_rows > 0) {
          $row = mysqli_fetch_array($result);
          $long = $row["long_url"];
          $id = $row["id"];
          $shorturl = $id;
          $sql = "UPDATE atomly_shortener SET short_url = '$shorturl' WHERE long_url='$longurl'";
            if ($conn->query($sql) === TRUE) {
                //echo "<script type='text/javascript'>alert('$shorturl');</script>";
                //echo "<script type='text/javascript'> document.getElementById('shorter').innerHTML = 'hello'; </script>";
            } else {
              echo "Error: " . $sql . "<br>" . $conn->error;
            }
      }
      else {
        echo "Unknown Error!";
      }
    }
    else {
      echo "Error shortening url, please try again!";
    }
  }
?>

<DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <div id = "container">
            <img id = "logo" src = "images/atomly_logo.png"></img>
            <form method="post" action="index.php">
                <fieldset class = "cf">
                    <input id = "url_long" type = "text" class = "shorten-input" placeholder = "Please enter your url..." name="long_url"></input>
                    <input id = "submit_" type = "submit" name = "submit_url" class = "action-btn" value = "SUBMIT"></input>
                </fieldset>
            </form>
            <div id ="shorter"><h1><?php if(isset($shorturl)){ echo "Short URL: localhost:81/atomly/".$shorturl; } ?></h1></div>
        </div>
    <body>
</html>

的.htaccess

RewriteEngine On
RewriteRule ^([^/]+)/? index.php?title=$1 [L,QSA]

未加载的图片在此行

<img id = "logo" src = "images/atomly_logo.png"></img>

1 个答案:

答案 0 :(得分:0)

您需要更新 .htaccess 文件,如下所示:

RewriteEngine On

RewriteRule !\.(?:jpe?g|gif|bmp|png|tiff)$ index.php?title=$1 [L,QSA]