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

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.



Solution 1:[1]

Protocol = #getPageContext().getRequest().getScheme()#;
Domain = #cgi.server_name#;
Template = #cgi.script_name#;
Variables = #cgi.query_string#;

So for example:

Canonical = '#getPageContext().getRequest().getScheme()#://#cgi.server_name#/#cgi.script_name#?#cgi.query_string#';

Solution 2:[2]

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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jules
Solution 2 Sushovan Mukherjee