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

后端 未结 4 2070
半阙折子戏
半阙折子戏 2021-02-19 22:01

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 TestSi

相关标签:
4条回答
  • 2021-02-19 22:18

    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.

    0 讨论(0)
  • 2021-02-19 22:20

    Since you tagged your question ASP.NET, I'm going to assume you want to do this server-side, so you are not relying on JavaScript on the client-side to generate HTML links.

    First of all, make sure you have a web.config file in your subdomain sites, and add this to the configuration:

    <configuration>
      <appSettings>
        <add name="MainDomain" value="example.com" /> 
      </appSettings>
    </configuration>
    

    Using this setting in your ASPX pages, hyperlinks on one of your subdomains' pages would look like this:

    <a href="<%$ AppSettings: MainDomain %>">Go to main domain home page</a>
    <a href="<%$ AppSettings: MainDomain %>/products.aspx">Go to products page on the main domain</a>
    

    The setting with name="MainDomain" will be read from the configuration file, and inserted into the page where it says <%$ AppSettings: MainDomain %>. This way, you are creating the required HTML on the server-side, with no hard-coding required.

    If you need to change the name, simply update the configuration file, and all pages will automatically use the new domain name.

    0 讨论(0)
  • 2021-02-19 22:26

    An alternate solution would be to use the original domain name for the live server, but point it to your development environment.

    You can override DNS lookups in Windows by adding lines to this file.

    C:\Windows\System32\drivers\etc\hosts

    Add the following using a text editor.

    127.0.0.1   www.livesite.com
    127.0.0.1   first.livesite.com
    127.0.0.1   second.livesite.com
    

    These domains will now map to the locahost. So there won't be a need to hack HTML for testing.

    0 讨论(0)
  • 2021-02-19 22:28

    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.

    0 讨论(0)
提交回复
热议问题