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.
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Alternate solution found
You'll find it is safer when you don't know the incoming case.
CGI.SERVER_NAME generally returns uppercase.