Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to main domain without hard-coding its name?

Tags:

html

asp.net

I have a domain name: TestSite.com. I create several subdomains for this site and refer to them as first.TestSite.com, second.TestSite.com, etc.

How do I refer to TestSite.com relatively without having to hard code its name in an html or aspx file in first.TestSite.com? What I mean is (using folders as example) if I have a folder TestSite and a sub folder first

TestSite/first

, then from first I can refer to its parent TestSite folder by

../

What do I use to refer to TestSite.com from first.TestSite.com? Thanks.

like image 981
RJIGO Avatar asked Apr 01 '12 04:04

RJIGO


People also ask

What is the word before a domain name called?

It's called a subdomain.

What is a hardcoded URL?

a) A hard-coded reference is a URL that contains the instance name in the URL (e.g. na1.salesforce.com). Replace these hard-coded references with generic, non-instance specific or relative URLs (e.g. login.salesforce.com or <mydomain>.

What is hardcoded domain in HTML?

It ties the security of your site to the security of the other site. Example: Consider the following script tag. <script src="http://www.example.com/js/fancyWidget.js"></script>

What characters are allowed in DNS names?

DNS names can contain only alphabetical characters (A-Z), numeric characters (0-9), the minus sign (-), and the period (.). Period characters are allowed only when they are used to delimit the components of domain style names.


2 Answers

There's no way using pure relative links. You have to program it as a string manipulation.

Something like:

var host = location.host;
var lastPeriod = host.lastIndexOf(".");
var remainder = host.substring(0, lastPeriod);
var afterSecondLastPeriod = remainder.lastIndexOf('.') + 1
var baseDomain = host.substring(afterSecondLastPeriod);
console.log(baseDomain);

EDIT: Shorter version using regex:

var baseDomain = host.match(/[^.]*\.[^.]*$/)[0]

This is general, so it will always return the last part. Regardless of whether it's a.TestSite.com, b.a.TestSite.com, etc. it will return TestSite.com.

You will have to modify it if this assumption is not correct.

like image 142
Matthew Flaschen Avatar answered Oct 28 '22 15:10

Matthew Flaschen


If you want to keep your relative links, you can use the base element.

[The base element's href] attribute specifies an absolute URI that acts as the base URI for resolving relative URIs.

So, add a base element, specify the href you'd like, and all the relative URIs on the page now use the URI you've specified as the base URI.

<head>
    <base href="http://testsite.com" />
</head>

That's all you need. However, if you'd like to make things a little cleaner, you can pull that URI from the web.config using System.Configuration.ConfigurationManager.AppSettings. Here's an aspx snippet:

<head>
    <base href="<%= ConfigurationManager.AppSettings["rootdomain"] %>" />
</head>

And the web.config:

<configuration>
  <appSettings>
    <add name="rootdomain" value="http://testsite.com" /> 
  </appSettings>
</configuration>

This method allows you to affect many elements from one and its value can be driven from the web.config.

like image 24
canon Avatar answered Oct 28 '22 17:10

canon