6

on http://www.exampleSite.com/aPage.php, the following image loads correctly

<img src="/images/sidenav/analysis-2.gif" />

but at http://localhost/exampleSite/aPage.php, it tries to get localhost/images/... instead of localhost/exampleSite/images/...

My file structure is:

C:\xampp\htdocs\exampleSite\

I have had this problem across multiple projects, and have previously resorted to absolute URLs, but now I am just trying to make some quick updates on a page, and I cannot view it correctly on my localhost.

Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149
  • Do you have any active mod_rewrite rules? – ComFreek Oct 19 '11 at 14:55
  • I have no .htaccess files in play, should I did deeper? – Zach Lysobey Oct 19 '11 at 15:06
  • In my situation, the reverse is the case, i.e. at http://localhost/exampleSite/aPage.php, it tries to get localhost/exampleSite/images/..., BUT I need to get localhost/images/... Any idea how this can be done? – Hamman Samuel Jan 02 '14 at 06:58
  • 1
    @HammanSamuel not really the best venue to ask that (better for a new question) but if your link starts with `/images` it *should* go to `localhost/images`. You can also experiment with `../images/path/to/file.jpg` (the `../` means "up a directory") – Zach Lysobey Jan 02 '14 at 15:33

2 Answers2

17

If you don't want to mess up with the src attributes throughout your website, you may consider changing configuration directives.

You can relocate it by editing the DocumentRoot setting in C:\xampp\apache\conf\httpd.conf.

It should be currently set as :

C:/xampp/htdocs

Change it to:

C:/xampp/htdocs/exampleSite

and your relative link as <img src="/images/sidenav/analysis-2.gif" /> should be working fine.

NOTE:

  • Don't forget to restart your XAMPP Server after making changes.
  • After these changes, your leading / will always direct to the exampleSite folder. Should you decide to change the root later, repeat the process for the root folder of your choice.
Ghazanfar Mir
  • 3,493
  • 2
  • 26
  • 42
  • 1
    So this will work as a temporary fix? Sounds like the best option so far. This means however that I cannot work on exampleSite, and exampleSite2 at the same time without changing the DocumentRoot back and forth – Zach Lysobey Oct 19 '11 at 15:19
  • 1
    TRUE. Changing your DocumentRoot is easier than src attributes throughout your website, i believe :) – Ghazanfar Mir Oct 19 '11 at 15:21
5

Remove the first / so that it becomes

<img src="images/sidenav/analysis-2.gif" />

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
  • 1
    You could also use `` or `` depending on how many directories you need to go up. – Dan Oct 19 '11 at 14:59
  • This isn't the reliable answer. OP wouldn't like to change the src between remote and local server while downloading/uploading. – Ghazanfar Mir Oct 19 '11 at 15:04
  • @GhazanfarMir His problem comes from the leading `/` since he doesn't seem to have the same directory structure between the local server and the remote one. – Nasreddine Oct 19 '11 at 15:06
  • Hmmm, I realize I misspoke before, and said quick updates on a 'page' when I should have done 'site'. There are multiple files invloved. I guesse I can do a bulk find-replace for '"/images'. Is there any other way you can think of to approach this? – Zach Lysobey Oct 19 '11 at 15:06
  • Depending on the structure of your website and the location of the html/php file you may be able to do a find/replace on all the files. You should take into consideration @DanLehmann's comment. – Nasreddine Oct 19 '11 at 15:10
  • I still don't understand why this discrepancy exists however. What is it precisely that the leading slash is doing? On my localhost it acts like ../ and on the webhost it is ignored? – Zach Lysobey Oct 19 '11 at 15:16
  • 1
    I think I answered my own question (sort of). The leading / apparently points to the document root (localhost/ or www.example.com/) rather than the current folder. – Zach Lysobey Oct 19 '11 at 15:31