How to get current page URL in coldfusion ? I am getting home_page_URL/index.cfm while fetching current page URL

Viewed 10511

I am using below code to get current page URL in coldfusion :-

host = structKeyExists(cgi,'http_host') ? cgi.http_host  : '';
req_url = 'https://' & host &  cgi.script_name;

But in req_url parameter I am getting host_name/index.cfm instead of current page URL. I am getting actual URL in referrer. Please let me know how to get current page URL in coldfusion.

2 Answers
property type="string" name="httpUrl" default="";

private string function getCurrentHttpUrl() {
    if( len( variables.httpUrl ) == 0 ) httpUrl = cgi.http_url;
    if( len( trim( variables.httpUrl ) ) == 0 ) {
        variables.httpUrl = lCase( getPageContext().getRequest().getScheme() );

        var port = ":" & cgi.server_port;
        if( ( variables.httpUrl == "http" &&  port == ":80" ) || ( variables.httpUrl == "https" && port == ":443" ) ) port = "";
        variables.httpUrl &= "://" & cgi.server_name & port;
        if( len( cgi.path_info ) > 0 ) variables.httpUrl &= cgi.path_info;
        else variables.httpUrl &= cgi.script_name;
        if( len( trim( cgi.query_string ) ) > 0 ) variables.httpUrl &= "?" & cgi.query_string;
    }
    return variables.httpUrl;
}
Related