gpt4 book ai didi

php - 使用 RewriteEngine 时图像未加载?

转载 作者:行者123 更新时间:2023-11-28 02:44:56 26 4
gpt4 key购买 nike

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

索引.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>

最佳答案

您需要像这样更新您的.htaccess 文件:

RewriteEngine On

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

关于php - 使用 RewriteEngine 时图像未加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com