1

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?

David
  • 1,227
  • 12
  • 23

2 Answers2

1

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.

Kzqai
  • 22,588
  • 25
  • 105
  • 137
  • I just found the same solution but maybe a better one. In HTML you can use the base tag to define the base for your links. would work for me. I think this is even more elegant. agree? – David Nov 13 '14 at 14:05
  • Haha, I have been down exactly the same path that you have. I experimented with using base in the past, and found there to be pitfalls in it's use. I'm still not sure whether base is worth the potential trouble enough to be useful. You can see some of the things that I went through on the question about the base tag here: http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag?lq=1 – Kzqai Nov 13 '14 at 16:23
  • Ah thanks man! So to recap, if base doesn't do it for me, i could always use "/" in front of my images. This way I would tell every image to link from the root of the server. It's still a bit nicer than to add the domain url in front of it. I kind of use this like Android. I have one resource folder, and every script should get it's resources from there. I think this solution is elegant, and will not give me any problems later on. At least i don't see any yet. Thanks for showing me the path you walked, it's been informative :) – David Nov 14 '14 at 12:18
0

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?

Jake S
  • 122
  • 14
  • I'm not sure if i understand you. 404 will be handeled by my index page. Also, the htaccess that i have already redirects to the index page. – David Nov 13 '14 at 13:39