We help IT Professionals succeed at work.

using ...src="/images/..." gets localhost instead of virtual directory on IIS

Jeremy Daley
Jeremy Daley asked
on
562 Views
Last Modified: 2013-12-13
i have a virtual directory in IIS called "bob" which is installed in my c:\inetpub\wwwroot\clients\bob folder

when i use <img src="/images/image.jpg"> it points all the way back to localhost instead of the virtual root "bob". i realize that using the forward slash at the beginning of src is doing this, but my intention is to have it look at the root. do i need to configure IIS in some way to have it point to "bob/images" instead of "localhost/images"?
Comment
Watch Question

If you're using ASP.NET and it's a server side control you can use the tilda ~ to signify the root of your application.

For example, if your application was in the virtual directory: http://localhost/bob

Then saying <asp:Image ImageUrl="~/images/Logo.gif" alternativeText="" /> would always display the image at http://localhost/bob/images/Logo.gif regardless of whether your page was in http://localhost/bob
or
http://localhost/bob/subdir1/subdir2/subdir3/subdir4

Also, if you then moved your application to a different virtual directory: http://localhost/harry/potter the url for the logo would automatically work with that as the new root... ie. it would look for the logo at http://localhost/harry/potter/images/Logo.gif.

I realise you're using PHP, but this may be some help to move to .NET? :o)

Hope this helps!
Jeremy DaleySenior Developer

Author

Commented:
i actually program in both PHP and .NET (C#), but to convert this site would be too tedious of a task. it will work fine once i upload it over to the actual host, but i'd like to test in on localhost first.

i guess this is more of an IIS issue than PHP, by the way it uses relative paths on virtual directories.
I think it's more likely to be a restriction of HTML rather than specifically PHP. .NET just works around the issue by allowing you to code something which represents 'root of app', and then at runtime, it figures out what the root is and translates it.

Viewing the HTML output of a page in http://localhost/bob/ which uses ~/images/logo.gif would show the image source as http://localhost/bob/images/logo.gif, not a normal relative path but instead an absolute URL.

It's been a while since I've coded in PHP, but you could create a function which does the same as the .NET tilda does... pop it in an file and include that one your pages that need to use the function. The function would return the root location of the website... I'd hope there's a way either view the request/response objects, or through an already existing PHP function to get the web-root? You may need to do some parsing, but at least if it's done in the function you're not having to recode that over and over.
Jeremy DaleySenior Developer

Author

Commented:
IIS on Windows XP Pro natively only allows you to create one site (localhost). If I was able to create a new site instead of just a virtual directory under "localhost", the preceeding forward slash would then take me to its root.

I just hope this doesn't require me to buy Windows Server 2003...
For testing/evaluation purposes you could always get an evaluation Virtual Hard Disk from:
http://www.microsoft.com/downloads/details.aspx?familyid=77f24c9d-b4b8-4f73-99e3-c66f80e415b6&displaylang=en

It'd allow you to run Windows 2003 Server to see if it does sort things for you before buying... if you did eventually have to.

Sorry I'm no further help.

Commented:
if the virtual dir is Called "Bob" then that is the start of the ref point if you want to reference anything in it.

<img src="bob/images/image.jpg">
Jeremy DaleySenior Developer

Author

Commented:
yes, but an included file is used in all pages "header.php"... and that means used in all other underlying directories (bob/states/ohio/somepage.php).

so <img src="bob/images/image.jpg"> would no longer be coming from the root "bob", but instead:
bob/states/ohio/bob/images/

make sense?

Commented:
Well this is the first you mentioned an include file,

Can you post all code?
You could always add a constant variable to your header.php file with the root... in essence the equivalent of putting the root address in the web.config?
Senior Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Closed, 500 points refunded.
Vee_Mod
Community Support Moderator
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.