Jump to content

Rewrite is working fine...


Fmdpa
 Share

Recommended Posts

I just implemented my first Apache RewriteRule into an .htaccess file:

RewriteEngine onRewriteRule image/([a-zA-Z-]+)/?$ image.php?image=$1

It changes this: www.site.com/image/sample-image...to this: www.site.com/image.php?image=sample-imageIt works fine, except that NO IMAGES are displayed, except for images that are inserted via the CSS stylesheet. There are no errors, so I can't figure it out. I checked the source code and the paths to the images were all correct...

Edited by Fmdpa
Link to comment
Share on other sites

I checked the source code and the paths to the images were all correct...
You mean if you copy and paste the path shown in the source of the page it won't display the image, but you're certain the path is correct?
Link to comment
Share on other sites

I found out via a debugger (why it wouldn’t show in the source, I don’t know) that it was actually searching for the images, and tacking on a non-existent “image” directory. So instead of searching for the image “uploads/sample-image.jpg”, it was looking for “image/uploads/sample-image.jpg”. What rewrite rule should I use that takes a url from this (note that dashes should be allowed in the image-name):http://site.com/image/sample-image…to this:http://site.com/image.php?image=sample-image

Link to comment
Share on other sites

RewriteRule ^/image/([a-zA-Z\-]+)/?$ /image.php?image=$1Which is almost exactly what you had. You might not need the first slash after the caret (^).I added a backslash in front of the last hyphen.The added directory may be in the PHP - can you post the PHP that assigns the name of the image file?

Link to comment
Share on other sites

Does this rewrite rule mean that the variable "$1" will hold this entire value: "/image/([a-zA-Z\-]+)/?" ? I only want the variable to hold the text after "image/"

Edited by Fmdpa
Link to comment
Share on other sites

Ok...here's the php code:

$image = isset($_GET['image']) ? mysqli_real_escape_string ($con, $_GET['image']) : '';$query = "SELECT * FROM images WHERE name='$image'";$result = $con->query($query) or die ('Database access failed: ' . mysqli_error());if ($result) {	while ($fetch = $result->fetch_object()) {		$title = $fetch->title;		$src = 'http://localhost/uploads/' . $fetch->src; //this is the image source		$description = $fetch->description;		$kwd = $fetch->keywords;		$award = $fetch->award;		$id = $fetch->id;	}		$src = preg_replace ('/ /', '%20', $src);

I echoed out the value of $src and got "http://localhost/uploads/sample-image.jpg", which is, in fact, a real directory. But the image still doesn't display. :)

Link to comment
Share on other sites

I created a very simple php page that tests whether this is working. Here’s the code for the page:

<?phprequire_once 'connect.php';$image = isset($_GET['image']) ? mysqli_real_escape_string ($con, $_GET['image']) : '';$query = "SELECT * FROM images WHERE image='$image'";$result = $con->query($query) or die ('Database access failed: ' . mysqli_error());if ($result) {	while ($fetch = $result->fetch_object()) {		$src = 'uploads/' . $fetch->src;}		$src = preg_replace ('/ /', '%20', $src);}$con->close();?><!DOCTYPE HTML>	<html>		<head lang="en">			<title>Untitled</title>		<link rel="Shortcut Icon" href="favicon.ico">				<meta charset="utf-8" />	</head>	<body>		<img src="<?php echo $src; ?>" />	</body>	</html>

I used this rewrite rule (the leading slashes didn’t help, wirehopper):

RewriteEngine onRewriteRule ^image/([a-zA-Z\-]+)/?$ sample.php?image=$1

I entered the address, http://localhost/image/sample-image/.The page showed, but there was no image. The page source said:

<!DOCTYPE HTML>	<html>		<head lang="en">			<title>Untitled</title>		<link rel="Shortcut Icon" href="favicon.ico">				<meta charset="utf-8" />	</head>	<body>		<img src="uploads/sample-image.jpg" />	</body>	</html>

I opened firebug and debugbar and found out in both that the SRC that the browser was actually looking for was image/uploads/sample-image.jpg. I further verified this by creating a folder “image” and placing “uploads/sample-image.jpg” in it. It displayed the image, no problem. I now know what the problem is (appended non-existent directory “image” on the url), but I can’t determine the cause or the solution. I have no other .htaccess files in the localhost root.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...