I have several websites that I help manage. When running on my local machine (under iis7), the websites run on under a folder on my machine d:\WebRoot\TheWebsiteName and run as htt://localhost/TheWebsiteName, but when they are deployed they run as http://www.TheWebsiteName.com (also running under iis7)

Currently I am handling the path to the website resources (images, javascript, etc) via code. But I was wondering if there was a better way to handle this rather than writing specialized code in the website.



For each of the websites setup a hostname in C:\Windows\System32\drivers\etc\hosts as a loopback to your local machine:    dev.thewebsitename.com    dev.thewebsitename.co.uk    dev.anotherwebsitename.com

this way you won't have to do any special mapping for your website resources. You can then setup multiple websites in IIS7 as you would do on your live webserver that only serve for the specified urls above. All you then need to do is change your web project's start url to be the new dev address and it will all be working.

I always then setup the different test servers exactly the same so all you need to do is change the subdomain to http://uat.thewebsitename.com etc. If you have a lot of sites you can also created a page with links to all the different sites and then have that bookmarked.

Also if you have a CDN for images you can either add that url as a loopback too, or just upload the images when developing. If you have them locally the speed will be great but you will need to make sure that you remove that loopback when you test.

This always worked like a dream for me and because it replicates your live servers and things like incorrect image/script urls will show up when developing. It does take a little more time to setup but is worthwhile and if you script it using PowerShell it won't be painful if you have to set it up again or for another developer.

| improve this answer | |
  • I really like this idea Caveman! – O.D. May 15 '12 at 18:27
  • +1 but I'd recommend thewebsite.local (otherwise your aliases may interfere with actual subdomains) – danlefree May 16 '12 at 21:43
  • @danlefree That's all very well if the OP's guys aren't using .local for the internal AD domain. If you use a subdomain and you run the website you should really know if that subdomain is used or not! :) In fact I used to use something that didn't look like a domain name at all lm-haysgb-ip00 (don't do much webdev now! :( ). – caveman_dick May 17 '12 at 8:55
  • Ok a workmate has just tweeted about readme.localtest.me which is a really great idea and means no modification of the hosts file is required. – caveman_dick Jun 27 '12 at 9:42

Presuming the resources are local, you should not need any special code at all -- just use relative paths to your resources and it will work in any environment. IE, use:

<script src = "scripts/whatever.js"></script>

Don't use something like:

<script src = "http://mylocalmachinename/scripts/whatever.js"></script>

If you are doing some CDN-like thing with a separate images domain then you will need some code somewhere.

| improve this answer | |
  • We do use a separate Cdn server for images, and it does help with some of of this. And also a CMS for the website content, the CMS returns the path as htt://localhost/TheWebsiteName – O.D. May 15 '12 at 18:25
  • The CMS should be adjusting itself to the environment then . . . – Wyatt Barnett May 15 '12 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.