Avatar of mopar003
mopar003
 asked on

Convert links for to querystring

I am trying to write a mobile version of a website, but am having problems passign the href tags and staying within my mobile version.

To make this successful, I need to rewrite all the href tags to pass through a querystring, so href=/mylink.asp now goes to href=/m/?r=mylink.asp.  Any URL has to be adjusted.  I tried this in the simple way by just finding any href tag, and adding the query id to it, but this does not account for root relative links vs page relative links.  See code below.  

I am sure I am not the first to try and do this, so any help would be very much appreciated.
<cfoutput>
    <!--- PASSES CURRENT PAGE URL, SET TO ABOUT FOR THIS SAMPLE--->
    <cfset url.r = 'aboutus.cfm'>
    <!---GET CONTENTS OF URL--->
    <cfset tempURL = "http://"&#CGI.SERVER_NAME#&#url.r#>
    <cfhttp url=#tempURL#></cfhttp>
    <cfset tempInfo = #cfhttp.filecontent#>
    
    <cfscript>
    //FIND STRING TO STRIP
    startPos = find('<h1 class', tempInfo);
    endPos = find('contentPrimary -->', tempInfo);
    cutLength = endPos - startPos;
    tempInfo = Mid(tempInfo, startPos, cutLength);
    
    //REPLACE any HREF that does not start with http
    
    //THIS IS WHERE I AM NOT SURE HOW TO PROCEED
    tempInfo = replace(tempInfo, 'href="/', 'href="/m/?r=/', 'all');		//REPLACES ALL HREF TAGS INTO QUERY STRING
	tempInfo = replace(tempInfo, 'href="/m/?r=http', 'href="http', 'all');	//REMOVES WHAT I JUST DID IF URL BEGINS WITH HTTP
    
    //STILL TO SOLVE
    //IF URL IS DOCUMENT RELATIVE
    //IF URL ALREADY CONTAINS A QUERYSTRING (CONVERT EXISTING ? to &)
    
    writeOutput(tempInfo);
	
    </cfscript>
</cfoutput>

Open in new window

ColdFusion Language

Avatar of undefined
Last Comment
mopar003

8/22/2022 - Mon
macrem

Have you tired ReplaceNoCase() ?
You'll find it is safer when you don't know the incoming case.
CGI.SERVER_NAME generally returns uppercase.

ASKER CERTIFIED SOLUTION
mopar003

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mopar003

ASKER
Alternate solution found
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23