Coldfusion-9 crop options

I am trying to write code that contains a URL that has three parts (www). (domainname). (com) and completely crop the first part.

So far I have this code that checks to see if I have “www” or “dev” on the left side, log in and install siteDomainName = removecharsCGI.SERVER_NAME,1,2);

if (numHostParts eq 3 and listfindnocase('www,dev',left(CGI.SERVER_NAME,3)) eq 0) {
        siteDomainName = removecharsCGI.SERVER_NAME,1,2);

The problem with the code above is that only 2 characters are deleted, where I need to delete ALL characters before numHostParts eq 2or at least the first "."

Another example might be:

akjnakdn.example.com I need code to remove the first part of the dotted URL (akjnakdn.)

This code will help some of the requests I have on the site to stop crushing because they are linked by C # URL #, and when # URL # is fake, I get an error cform query returned zero recordsthat causes my contact forms to stop working.

+3
source share
3 answers

You can do something like this:

<cfscript> 
    local.nameArr = ListToArray(CGI.SERVER_NAME, '.');
    if (ArrayLen(local.nameArr) gt 2) {
        ArrayDeleteAt(local.nameArr, 1);
    }
    siteDomainName = ArrayToList(local.nameArr, '.');
</cfscript>

I split the server name into array elements with a period as a separator. If the number of elements is more than two, delete the first element. Then translate it back to the list with the period as a separator.

UPDATE

As suggested by Robb, this can be more concise and work better by skipping the process of converting an array:

<cfscript> 
    siteDomainName = CGI.SERVER_NAME;
    if (ListLen(siteDomainName, '.') gt 2) {
        siteDomainName = ListDeleteAt(siteDomainName, 1, '.');
    }
</cfscript>
+4
source

listRest. , . http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d87.html

:

<cfscript>
name = cgi.server_name;
if (listlen(name,".") gte 3) {
  name = listRest(name,".");
}
</cfscript>
+6

I would use a regex because you want to "crop" certain subdomains ( www, dev).

<cfset the_domain = REReplaceNoCase(cgi.SERVER_NAME, "(www|dev)\.", "") />

Just use the |-delimited list of subdomains that you want to trim in parentheses.

0
source

All Articles