David
David

Reputation: 1246

Reroute to index with working images

I'm working on a site where i want to route all communications trough the index file. I have a simple test project with the following architecture:

root-   
   -.htaccess   
   -index.php   
   -icon.jpg

My .htacces is as follows:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ index.php/$1 [L]

this works, now can visit www.mysite.com/abc/def/ghi and still the index.php page shows.

I can use abc/def/ghi by retrieving the $_SERVER[ 'REQUEST_URI' ].

However, lets say i this in my index.php:

echo "<img src='icon.jpg'>";    

the icon will not show up because the server will search for it in:

www.mysite.com/abc/def/ghi/icon.jpg

instead of

www.mysite.com/icon.jpg

What am i doing wrong, and how would one solve this problem professionaly?

Upvotes: 1

Views: 35

Answers (2)

Kzqai
Kzqai

Reputation: 23082

Simply enough, you should use absolute pathing instead of relative pathing for your images.

One way to resolve this is to define a global constant for the project, like:

define('IMAGES', 'http://website.root.domain.com'.'/images/');
OR
define('IMAGES', DOMAIN_URL.'/images/');

And then use that in your output code like:

<img src='<?php echo IMAGES;?>icon.jpg'>

And the output would result in the complete & unalterable path like:

<img src='http://website.root.domain.com/images/icon.jpg'>

Note that I've taken the liberty of assuming you'll be storing images in their own personal directory as opposed to in the main public directory.

Upvotes: 1

Jake S
Jake S

Reputation: 142

This doesn't answer the question, but why can't you create a 404 error page and just set up a redirect up to the index page?

Upvotes: 0

Related Questions