Daniel Smith
Daniel Smith

Reputation: 3

PHP Fix Relative Image Path Using .htaccess

I have a php file, for example www.example.com/folder/test.php

The file above (test.php) has the line

include($_SERVER['DOCUMENT_ROOT'].$page);

$page = /folder2/.$directory1./.$directory2./.index.php

$directory1 and $directory2 are all random.

However,

The index page has an image on it that just references the image as the source.

For Example src="image.bmp"

So what happens is the image's url actually shows up like this... www.example.com/folder/image.bmp

And it should show up like this... www.example.com/folder2/.$directory1./.$directory2./image.bmp

Is there a way to fix this besides using absolute paths? There are millions of directories and we would like to correct the issue rather than correct the problem (even though we will do both).

Can I place an htaccess file in the directory its looking for the image in to redirect it?

FROM: www.example.com/folder/

TO: www.example.com/folder2/.$directory1./.$directory2./

Upvotes: 0

Views: 631

Answers (1)

entropid
entropid

Reputation: 6239

If you want to do that, you should either dynamically change the .htaccess or specify in it any possible $directory1/$directory2 combination. In my opinion, the best way to do this is to change the src of that image in the file index.php to something like:

<img src="/folder2/<?php echo $directory1; ?>/<?php echo $directory2; ?>" />

Upvotes: 1

Related Questions