coldfusion concatenate strings

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!
Bang-O-MaticAsked:
Who is Participating?
 
_agx_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>
0
 
_agx_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.
0
 
Bang-O-MaticAuthor 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!!
0
 
_agx_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.
0
 
Bang-O-MaticAuthor Commented:
Thanks _agx_! worked perfect!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.