Copy link to clipboard
Copied
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>
I
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.serv
...
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
...
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
When I reload the page it behaves normally. There is no rediraction. It just stands there. I am unsure where to look up. I am checking network tab and there are like 50 things going on there, from css files, over js files, documents, images, etc.
I attached image of the code where I placed it. It's a cfm file called "website-header-top-template". This is a template which is connected with other templates to make a whole page. Your code is added on the top of the page on all pages.
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Oh, something just crossed my mind. Are you implementing the solution in Application.cfc? For example, in onRequestStart()?
Copy link to clipboard
Copied
It's added in my header cfm file. Where the actual page starts (before doctype declaration). Should I relocate?
Copy link to clipboard
Copied
No need to relocate. Header.cfm is customarily included via Application.cfc (unless you've implemented it differently).
Copy link to clipboard
Copied
I mean the code works but I only need to remove from URL last /index.cfm. That's all but I don't know how?
Copy link to clipboard
Copied
Ha ha, I thought you only mentioned that as being a challenge to do. Does this help?
<cfscript>
uri="https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-l/listsetat.html";
nrUriParts=listLen(uri, "/");
uriWithLastPartReplaced=listSetAt(uri, nrUriParts, "", "/");
writeoutput("<strong>uri:</strong> " & uri & "<br>" );
writeoutput("<strong>uriWithLastPartReplaced:</strong> " & uriWithLastPartReplaced );
</cfscript>
Copy link to clipboard
Copied
We misunderstood what another, probably because English is not my native language. A code you gave me above works like a charm. My url is always lowercase and that's what I wanted but something happened after I applied the code. Before adding it, my URL never had /index.cfc in it. Now it does. All I need is to keep lowercase as your code does, but also remove that file name. No more, no less. I hope I am being clear now 😄
Copy link to clipboard
Copied
You have been clear all along. We only misunderstood each other.
Please share the part of the code that you are using right now. Also share the result you are currently getting. Then tell us what you wish to get.
Copy link to clipboard
Copied
Here is the code.
<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>
And here is the result: https://test.almarsguides.com/retro/index.cfm
when you go any sub folder, it shows index.cfm
I need that out.
Copy link to clipboard
Copied
So there are two requirements to the problem: (1) strip off "/index.cfm"; and (2) redirect to lower-case URI when browser requests upper-case URI.
You could expand your present code by adding a line that finds and removes "/index.cfm" . For example,
<cfscript>
if (trim(cgi.server_port) neq "") {
port=":" & cgi.server_port;
} else {
port="";
}
// The URI
canonical = '#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#';
if (trim(cgi.query_string) neq "") {
canonical = canonical & "?#cgi.query_string#";
}
uriContainsIndexCFM=findNoCase("/index.cfm", canonical) != 0;
//The lower-case version of the URI
lowerCaseCanonical=lCase(canonical);
</cfscript>
<!--- When URI contains /index.cfm, remove it, then rdirect to resulting lowercase-URI --->
<cfif uriContainsIndexCFM>
<cfset uriWithoutIndexCFM=replaceNoCase(lowerCaseCanonical, "/index.cfm", "")>
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="#uriWithoutIndexCFM#">
<cfabort>
</cfif>
<!--- When URI and lowercase-URI don't match, redirect to lowercase URI --->
<cfif compare(canonical, lowerCaseCanonical)>
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="#lowerCaseCanonical#">
<cfabort>
</cfif>
Copy link to clipboard
Copied
Now it shows page is not working. When I apply the code given.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
To summarize of my last post:
Copy link to clipboard
Copied
Ok, let me put it this way. Without using script that worked for me (making url lowercase), my URL is always displayed without index.cfm in the end. It is always whatever.com/test/blabla/
However, when I use the script that truly does make my url lowercase, then index.cfm appears.
The script that makes an issue afterwards (which doesn't work), just tells me that page doesn't work. This script:
<cfscript>
if (trim(cgi.server_port) neq "") {
port=":" & cgi.server_port;
} else {
port="";
}
// The URI
canonical = '#getPageContext().getRequest().getScheme()#://#cgi.server_name##port##cgi.script_name#';
if (trim(cgi.query_string) neq "") {
canonical = canonical & "?#cgi.query_string#";
}
uriContainsIndexCFM=findNoCase("/index.cfm", canonical) != 0;
//The lower-case version of the URI
lowerCaseCanonical=lCase(canonical);
</cfscript>
<!--- When URI contains /index.cfm, remove it, then rdirect to resulting lowercase-URI --->
<cfif uriContainsIndexCFM>
<cfset uriWithoutIndexCFM=replaceNoCase(lowerCaseCanonical, "/index.cfm", "")>
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="#uriWithoutIndexCFM#">
<cfabort>
</cfif>
<!--- When URI and lowercase-URI don't match, redirect to lowercase URI --->
<cfif compare(canonical, lowerCaseCanonical)>
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Location" value="#lowerCaseCanonical#">
<cfabort>
</cfif>
Copy link to clipboard
Copied
Then use the script that worked, with the following modification:
// The URI ends 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 URI ends 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", "");
}
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you very much for all the help given. All is good now. ^^
Copy link to clipboard
Copied
My pleasure, @Aleksandar34715023f1z9 .