Skip to main content
Inspiring
January 24, 2024
Answered

Make full website URL to become lowercase

  • January 24, 2024
  • 3 replies
  • 4384 views

I am having a website with URL where subfolders vary from all uppercase to all lowercase. What I would like to happen is to make whole website URL lowercase once loaded. Even if the user changes it to whatever he wants, it will revert to lowercase once page refreshed. 

 

I tried using what ChatGPT said but this only gives me 500 Internal error. He tried many different variations of this and with http and https and yet all fail in the same way. He also tried with haccess file but it didn't work. Actually, it works for first subfolder, but fails for all others in the path.

 

<cfsetting showdebugoutput="Yes">
<cftry>
    <cfset requestURL = GetHttpRequestData().headers["host"] & GetHttpRequestData().headers["x-original-uri"]>

    <cfset lowercaseURL = LCase(requestURL)>

    <cfset segments = ListToArray(lowercaseURL, "/")>

    <cfset lastSegment = ArrayLast(segments)>

    <cfif CGI.QUERY_STRING NEQ "">
        <cfset lowercaseURL = "#Replace(lowercaseURL, lastSegment, LCase(lastSegment))#?#CGI.QUERY_STRING#">
    </cfif>

    <cfset lowercaseURL = ReplaceList(lowercaseURL, "/index.cfm", "", "ALL")>

    <cfif LCase(CGI.SCRIPT_NAME) neq LCase(lastSegment) AND LCase(CGI.SCRIPT_NAME) neq LCase(ListLast(CGI.SCRIPT_NAME, '/'))>
        <cfheader statuscode="301" statustext="Moved Permanently">
        <cfheader name="Location" value="#lowercaseURL#">
        <cfabort>
    </cfif>

 

    This topic has been closed for replies.
    Correct answer BKBK

    Can you give me full code from the script that worked? I really have no idea how any of this work, honestly. You did give me snippets but I have no idea where to put it and what to remove. 

     

    Right now I got this one and it seems to work. No idea how it works, but it seems it does. 

    <cfscript>
        if (trim(cgi.server_port) neq "") {
            port = ":" & cgi.server_port;
        } else {
            port = "";
        }
    
        // The URI without the filename
        if (trim(cgi.query_string) eq "") {
            canonical = '#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#';
        } else {
            canonical = '#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#?#cgi.query_string#';
        }
    
        // Remove "/index.cfm" from the canonical URL
        canonical = replaceNoCase(canonical, "/index.cfm", "", "all");
    
        // The lower-case version of the URI without the filename
        lowerCaseCanonical = lCase(canonical);
    </cfscript>
    
    <!--- Your redirect code: redirect only when URI and lowercased URI don't match --->
    <cfif compare(canonical, lowerCaseCanonical)>
        <cfheader statuscode="301" statustext="Moved Permanently">
        <cfheader name="Location" value="#lowerCaseCanonical#">
        <cfabort>
    </cfif>

     


    Just a matter of replacing the snippet in the code that you yourself say works. Namely:

    <cfscript>
    	if (trim(cgi.server_port) neq "") {
    		port=":" & cgi.server_port;
    	} else {
    		port="";
    	}
    // You want the the URI to end in .../potato
    if (trim(cgi.query_string) eq "") {
    	canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#", "/index.cfm", "");
    } else {
        canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#?#cgi.query_string#", "/index.cfm", "");
    }
    // You want the the URI to end in .../potato/
    /*
    if (trim(cgi.query_string) eq "") {
    	canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#", "index.cfm", "");
    } else {
        canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#?#cgi.query_string#", "index.cfm", "");
    }	
    */
    	//The lower-case version of the URI
    	lowerCaseCanonical=lCase(canonical);
    </cfscript>
    
    <!--- Your redirect code: redirect only when URI and lowercased URI don't match --->
     <cfif compare(canonical, lowerCaseCanonical)>
        <cfheader statuscode="301" statustext="Moved Permanently">
        <cfheader name="Location" value="#lowerCaseCanonical#">
        <cfabort>
    </cfif>

     

     In any case, did you see my numbered questions in the previous post? I think that if you answered them we would solve the problem quickly. 

    3 replies

    Leather372091001nd3
    New Participant
    May 6, 2024
    
    <cfscript>
    	if (trim(cgi.server_port) neq "") {
    		port=":" & cgi.server_port;
    	} else {
    		port="1";
    	}
    // You want the the URI to end in .../potato
    if (trim(cgi.query_string) eq "") {
    	canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#", "/index.cfm", "");
    } else {
        canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#?#cgi.query_string#", "/index.cfm", "");
    }
    // You want the the URI to end in .../potato/
    /*
    if (trim(cgi.query_string) eq "") {
    	canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#", "index.cfm", "");
    } else {
        canonical=replaceNoCase("#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#?#cgi.query_string#", "index.cfm", "");
    }	
    */
    	//The lower-case version of the URI
    	lowerCaseCanonical=lCase(canonical);
    </cfscript>
    
    <!--- Your redirect code: redirect only when URI and lowercased URI don't match --->
     <cfif compare(canonical, lowerCaseCanonical)>
        <cfheader statuscode="301" statustext="Moved Permanently">
        <cfheader name="Location" value="#lowerCaseCanonical#">
        <cfabort>
    </cfif>

     

    Hi,

    I'm very new to these code stuff, but I tried using your given code on my website's theme headers through a plugin for code inserter, and I'm getting errors on my website, so often, and I removed the code inserter plugin as well, tried installing it again but still couldn't find where the code is added, I can just remember that the code insert had a header option and few more, I selected header. My website (LeatherDiscover) is on WordPress with a GeneratePress theme, please ask if there are more info needed. and your help will be really appreciated. Thanks

    BKBK
    Braniac
    May 7, 2024

    @Leather372091001nd3 , You've not said what errors you are getting. Anyway, from what you have said, it might be a good idea to ask your question in a new thread. If you do, include the errors you are getting.

    BKBK
    Braniac
    January 27, 2024

    In place of GetHttpRequestData(), you could use the CGI variables. You could, for example, borrow from Jules' definition of URI on Stackoverflow, which makes use of CGI.

     

    So we could start with the following idea. As you can see, I felt free to add a correction for the port.

     

    <cfscript>
    	if (trim(cgi.server_port) neq "") {
    		port=":" & cgi.server_port;
    	} else {
    		port="";
    	}
    	// The URI
    	if (trim(cgi.query_string) eq "") {
    		canonical = '#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#';
    	} else {
    		canonical = '#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#?#cgi.query_string#';
    	}
    	
    	//The lower-case version of the URI
    	lowerCaseCanonical=lCase(canonical);
    </cfscript>
    
    <!--- Your redirect code: redirect only when URI and lowercased URI don't match --->
     <cfif compare(canonical, lowerCaseCanonical)>
        <cfheader statuscode="301" statustext="Moved Permanently">
        <cfheader name="Location" value="#lowerCaseCanonical#">
        <cfabort>
    </cfif>

     

     

    Inspiring
    January 27, 2024

    This is almost perfect answer. One thing left is to remove /index.cfm part at the end of the URL. So far it goes: "blabla.com/test/potato" and it stops there. With the code you gave me, it is "blabla.com/test/potato/index.cfm" 

    I just need the last one removed. 

     

    Thank you again for this. Works like a charm. (Just one more left to fix)

    BKBK
    Braniac
    January 29, 2024

    Oh, something just crossed my mind. Are you implementing the solution in Application.cfc? For example, in onRequestStart()?

    Braniac
    January 24, 2024

    If I'm understanding you correctly, you want to redirect users to a new URL that contains only lower-case letters?

     

    Try something like:

     

    <cfset userURL = "https://" & cgi.HTTP_HOST & cgi.HTTP_URL>
    <cfif reFind("[A-Z]", userURL) GT 0>
    	<cflocation url="#lcase(userURL)#" addtoken="No">
    </cfif>

     

     

    Use caution.  There could be situations where this causes an inifite redirection loop?

     

    Inspiring
    January 24, 2024

    I've tried what you gave me but it seems nothing has changed in my URL. After I added your code my URL is: 
    whatever.com/retro/walkthroughs/GBA/

    No lowercase . 

     

    I added this Before "<!doctype html>" . Or should I add this somewhere else? I am trying to apply this to all pages considering this is a template for all pages. 

    Braniac
    January 24, 2024

    OK, I'll need you to provide more info if you need more help.

    Have you looked in your browser developer tools....specifically, the Network tab?  On a test page I built with this code, when I load it I see 2 log entries:
    302 GET

    200 GET

     

    The 302 shows that it landed on the page, detected a capital letter, and redirected to the 'new' URL that is all lower-case.  Do you see a 302 GET?

     

    Do you see a redirect happening?