7/25/2009 Webmaster

DNS, Portal Alias and the Hosts file.


I have seen more than one person on the DotNetNuke forums faced with a problem when trying to access their portal. Only to find out that they had moved the portal or changed the hostheader, DNS etc. for the url associated with the portal. As it turns out, the root of the problem is that the Portal Alias is now “out of sync” with the url they are using to access the portal. There are two ways to fix this, that I know of. Updating the records in the PortalAlias table in the DotNetNuke database or pointing the original url back to the portal to align with the portal alias records.

Updating the PortalAlias table should be pretty self explanatory for the most part. Simple open the table in SQL Management Studio and edit the value in the HTTPAlias column to the url you are needing to use for the portal.

The other option of pointing the original url back at the portal may not always be possible. However, there is some trickery that can be used to emulate the original url. This is a well documented and widely used practice across platforms. You may have guessed it by now. Yes I am referring to the hosts file.

On windows it is located in %windir%\system32\drivers\etc and is simply called hosts. It is a standard text file with a description at the top and a list of tab separated key \ value pairs. In this case, its more like a value\key pair. The value, that is the IP address, is actually listed first and then the hostname is entered after a tab character. Here is an example:

127.0.0.1             localhost

127.0.0.1             dnn.dev

So, for the sake of discussion, say you had a portal that was configured with a portal alias of sampledomain.dev and for one reason or another you can no longer access that domain or do not have control to point it to a new server. You can temporarily point sampledomain.dev to your own pc or development server etc. Of course this only applies to your computer and does not have any really effect on the actual domain information. Which is a good thing. Once you have entered 127.0.0.1[Tab]sampledomain.dev into the hosts file and saved your changes. Launch PowerShell or the Old School DOS window and run the following command: nbtstat –R  You should see this message: Successful purge and preload of the NBT Remote Cache Name Table.

At this point, you can launch a new instance of IE or your browser of choice and navigate to sampledomain.dev. It should now skip DNS resolution and go directly to the IP address you specified in the hosts file.

I am sure many of you already know all about the host file and how it works. However, it appears that it is still drastically underutilized by most developers. Personally, if I am doing any kind of serious web development, I want to use IIS to host my development site. This is especially true when developing DotNetNuke. Cassini just doesn’t cut it for me when doing large scale web apps. So I frequently add and remove entries from my host file to emulate portals or recover them from other computers. Below is a PowerShell script to handle all of this in one easy command. This script is not the best piece of code I have written and honestly I am a hobbyist at best when it comes to PowerShell, but it gets the job done.

[string]$hostsPath = "c:\windows\system32\drivers\etc\hosts"
[string]$ip = $args[0]
[string]$hostname = $args[1]
[System.Collections.ArrayList]$lines = new-object System.Collections.ArrayList
$lines.AddRange([IO.File]::ReadAllLines($hostsPath))
$lines.Add($args[0] + "`t" + $args[1])
[IO.File]::WriteAllLines($hostsPath, $lines.ToArray([string]))
if($args[2] -eq "exit"){
  Write-Host Host added.
}elseif([string]::IsNullOrEmpty($args[2])){
  nbtstat -R        
  Write-Host Host added and refreshed.
}else{
  nbtstat -R        
  [string]$iis = $args[2]
  [string]$notepad = $args[3]
  if($iis -eq "Y"){c:\windows\system32\inetsrv\inetmgr.exe}
  if($notepad -eq "Y"){notepad.exe $hostsPath}
   Write-Host Host added and refreshed.
  
}
 

If you save this script as Add-Host, here are some examples of how to run it:

Add-Host 127.0.0.1 localsite.dev y - Will add an entry to your hosts file, refresh you local DNS, and launch the IIS manager (this is useful if you need to configure the host header or binding in IIS)

Add-Host 127.0.0.1 localsite.dev y y - You can also launch notepad with the hosts file loaded by adding a second y

Add-Host 127.0.0.1 localsite.dev exit - To prevent your local DNS from being refreshed you can enter (this will also prevent IIS and notepad from launching):

 

I truly hope that this little trick adds a useful tool to your toolbox, and if it was already there, hopefully I have encouraged you to make the best use of it!

An error has occurred. This application may no longer respond until reloaded. Reload 🗙