Wednesday, July 1, 2015

Configure HostHeaders for SharePoint Web Application in Dev Boxes.

Hi,

How bad it looks when developing our applications in our dev boxes we use url's like http://xyz-servername:port/

With 2013 to configure apps we need to have some DNS entries created. But traditionally not every developer care to have dns entries created for web applications in dev boxes.

Also learning from my past lessons with different urls in Dev and Production for the simmilar sites, sometimes you have to manually change the urls in infopath forms, workflows to republish the working stuff.

To make things better, here is the way i am sharing on how to configure host header and edit bindings to let the host header work for you without configuring anything on DNS.

We will use Powershell to modify the host file and will also set bindings on IIS to make WebApplication availlable outside the server.

Here are the steps:-

  1. Create a WebApplication (You can extend one if you want to override the existing one).
    I just have created a normal web application with host header "ankurmadaan" and port i used is 80. Note:- i allready have a sharepoint default web application on port 80.
  2. Now we need a root site collection,  I like to keep my site collections in there own database, for me it will be root collection so it will go into default database.

  3. So now if you try to browse, you will be surprise to see that your site does not works, unless you have an entry for http://ankurmadaan in DNS mapped to ip of your server.
  4. Also you will be loop back and Authentication Required pop up will keep coming.
  5. So here is the solution, we will write Powershell Script to make this work:-
  6. First we will write script to get path of hosts file and create a copy of it:-
    #Make backup copy of the Hosts file with today's date 
    $hostsfile = 'C:\Windows\System32\drivers\etc\hosts'
    $date = Get-Date -UFormat "%y%m%d%H%M%S"
    $filecopy = $hostsfile + '.' + $date + '.copy'
    Copy-Item $hostsfile -Destination $filecopy
    
    
  7. Above code will create a copy of host file for safe way purpose.
  8. Next we will add entry for "ankurmadaan" in it:-
    Write-host "Adding entry for webapplication" 
    add-content -path $hostsfile -value "127.0.0.1 `t ankurmadaan"

  9. Last but not the least if not done allready in your server, entry for LoobBackCheck:-
    # Disable the loopback check
     New-ItemProperty HKLM:\System\CurrentControlSet\Control\Lsa -Name "DisableLoopbackCheck" -Value "1" -PropertyType dword

  10. So here it is the working http://ankurmadaan/ :-
    
  11. If you have multiple webapplications and you want to do this entire step in one go, in that case you can use alternate access mappings, here is the script for it:-

    # Get a list of the AAMs and weed out the duplicates $hostsfile = 'C:\Windows\System32\drivers\etc\hosts' $hosts = Get-SPAlternateURL | ForEach-Object {$_.incomingurl.replace("https://","").replace("http://","")} | where-Object { $_.tostring() -notlike "*:*" } | Select-Object -Unique # Get the contents of the Hosts file $file = Get-Content $hostsfile $file = $file | Out-String # write the AAMs to the hosts file, unless they already exist. $hosts | ForEach-Object { if ($file.contains($_)) {Write-Host "Entry for $_ already exists. Skipping"} else {Write-host "Adding entry for $_" ; add-content -path $hostsfile -value "127.0.0.1 `t $_ " }}
  12. Now, this will work when you are inside the servers, what will you do to have it accessed by users from outside of the server in your network, like you want to demo it someone, so here is the way to do it:-
  13. Go to IIS  and right click on this Web Application and click on Edit Bindings:-

  14. Add a new entry and specify the port name and add server name(current server) on which you are configuring this, WFE if your dev box has multiple servers:-




  15. Click ok and the last step is to create an extra alternative access mapping. Open Central Admin ->Configure alternate access mappings -> Add Internal URLs -> Change alternate access mapping location to this web application.

  16. Add internal url http://servername:93 what we used in step 14. also select custom for zone
  17. Now browse http://servername:93 , it will open the same site as http://ankurmadaan will open in the server.


    NOTE:- you have to configure hosts file in each of the server connected to your dev box but binding can be done on any WFE to expose the site outside of the server.


Thanks.

No comments:

Post a Comment