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
I am sorry to bother you again but new issue I have now may be affected by the script I am using. You see, this is the script I am using:
<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>
Now, this is the issue I am getting. How can I have 85k page unindexed?
Copy link to clipboard
Copied
No problem. You bother no one. This is after all a discussion forum. 🙂
It appears to me that what you describe is not actually an issue.
If you do a 301 redirect from URL_1 to URL_2, it is common practice for Google Index to no longer index URL_1, but to index URL_2. However, an advice I have heard is that you should not remove or block URL_1 in Google Search Console.
In any case, I cannot see what we can do about this with ColdFusion. You will get more, and better, advice from Google Support. See, for example,
https://developers.google.com/search/docs/essentials
Copy link to clipboard
Copied
<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
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
Leather, you say your site runs in WordPress (which runs on php) , but you're referring here to coldfusion code--and indeed you're asking on a coldfusion discussion forum. Unless you can explain the connection between the two, I think you're mistaken in using this code at all. While both Cf and php can be used to create dynamic web sites, they're entirely different languages from each other.
Copy link to clipboard
Copied
WordPress runs on PHP, not CFML. You'll have to find a comparable forum discussing PHP and WP. I'm sorry I can't provide more help.
Dave Watts, Eidolon LLC
Copy link to clipboard
Copied
Oops, sorry, I didn't see Charlie's response.
Dave Watts, Eidolon LLC