ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 CoverYourASP --> The base href tag --> Part 2

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
314 active users
1868 visitors today
2826 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

CoverYourASP
Copyright © 1999-2008 James Shaw.
All rights reserved.

ASP.NET Blog
ASPRSS
ShawThing

Now open source with SourceForge!

Using <base href>

The answer was to use <base href>. This tells the browser what URL relative links are relative to. So if I use:

<base href="http://CoverYourASP.com/">

<a href="Hello.asp">Say hello</a>

The link would always point to http://CoverYourASP.com/Hello.asp, no matter where this page was.

Configuring <base href>

Of course, hard-coding http://CoverYourASP.com/ into my code wouldn't help you or I very much. The <base href> needs to change when it's on the development server - in my case I want it to be http://localhost/cya/.

This is accomplished with some code in utils/Init.asp, shown below:

var sServer = '' + Request.ServerVariables ( 'SERVER_NAME' );

// if running securely, use https://
var sRoot = '' + Request.ServerVariables ( 'HTTPS' );

var sBaseServer = 'http' + ( sRoot == 'on' ? 's' : '' ) + '://' + sServer;

// if running on non-standard port, use it
var nPort = Request.ServerVariables ( 'SERVER_PORT' ) - 0;

if ( nPort != 80 )
   sBaseServer += ':' + nPort;

// test which URL I am running from
for ( var i=0; i<sURLTest.length; i++ )
{
   if ( -1 != sBaseServer.indexOf ( sURLTest [ i ] ) )
   {
      // apply the offset if one given
      if ( sURLOffset [ i ].length )
         sBaseServer += '/' + sURLOffset [ i ];
      break;
   }
}

Out ( '<base href="' + sBaseServer + '/" target="_top">' );

To start with I get the server name from Request.ServerVariables. Giving my servers as an example, I get either "CoverYourASP.com" or "localhost" from this.

I then test to see if the page was requested with SSL security, and if so use https:// instead of http://

If the page was accessed using a different port (not the default port 80) then that is appended too.

Part 3: The sURLTest array...

Featured sponsor
My favorite resources

Selling products online - read how James does it now



New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!