We help IT Professionals succeed at work.

coldfusion concatenate strings

Bang-O-Matic
Bang-O-Matic asked
on
I want to grab a url like this one:
http://mydomain.com/12543/mystreetnumber-streetname.html
and 301 redirect it to
http://mydomain.com/A12543/mystreetnumber-streetname.html

I'm trying to figure out how to prepend the A to the  cgi.script_name

Here is what I have tried:

<cfset mlsidentifier = "A">
<cfset test = mlsidentifier & cgi.script_name>


but that just outputs:    
A/12543/mystreetnumber-streetname.html

where I need
A12543/mystreetnumber-streetname.html

Is it possible to prepend a character to the cgi.script_name?

Any help appreciated!
Comment
Watch Question

Most Valuable Expert 2015

Commented:
A simple regex like this might do it:

<!--- If the 1st character is "/", replace it with "/A" --->
<cfset oldPath = "/12543/mystreetnumber-streetname.html">
<cfset newPath = reReplace( oldPath, "^(/)", "\1"& mlsidentifier )>
<cfoutput>#newPath#</cfoutput>

You could also use list functions.  Though if the script_name path can vary from what you posted, might want to add some validation to handle unexpected case.
Most Valuable Expert 2015
Commented:
Something like this should work as well. Just throw in validation in case of an empty path

     <cfset oldPath = "/12543/mystreetnumber-streetname.html">
     <cfset newPath = listSetAt(oldPath, 1, mlsidentifier & listFirst(oldPath, "/"), "/")>
     <cfoutput>#newPath#</cfoutput>

Author

Commented:
Hi _agx_ I havent had a chance to test this, but yes these are all dynamic URL's that I want to redirect.

Looking at your code sample:  oldpath = cgi.script_name

So this should work, correct?
<cfset mlsidentifier = "A">
<cfset oldPath = cgi.script_name>
<cfset newPath = listSetAt(oldPath, 1, mlsidentifier & listFirst(oldPath, "/"), "/")>
     <cfoutput>#newPath#</cfoutput>

Thanks for the help on a Saturday!!
Most Valuable Expert 2015

Commented:
Sorry, I was away from the gadgets for a while :)

I can't test it right now, but that looks fine. (I just hard coded the path for demo purposes). Though you may want to add some validation for unexpected paths.

Author

Commented:
Thanks _agx_! worked perfect!