Wife/Girlfriend/Sister?
Pepper Spray &
Stun Gun Specials!
KEEP THEM SAFE.
 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
19 active users
18 visitors today
57 pages today
how is this done?
Tools I use

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

ASP.NET Blog
RSS submissions
E-commerce

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

Tiki Statues - Tiki Masks - Tiki Totems



See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!