• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Redirection for pages that no longer exist

Explorer ,
May 11, 2024 May 11, 2024

Copy link to clipboard

Copied

So, hello again. On my website inside my root folder I had folders for some guides, however I moved them to another location. What I would need now is when they try to go to https://almarsguides.com/Aion/ , they are moved to https://almarsguides.com/Computer/Games/Aion/ . Because right now they will be redirected to page 404.

 

One important thing to note is that this should work for all guides. If let's say they go to https://almarsguides.com/Aion/professions , they should be redirected to https://www.almarsguides.com/Computer/Games/Aion/professions/

 

I will have to do this for other folders too, but this is the main idea. Is it possible to do this in Application.cfc because it seems that my htaccess doesn't work well?

Views

175

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2024 May 11, 2024

Copy link to clipboard

Copied

Aleksandar, before answering, are you more a business person or a programmer? Do you only manage this site, I mean? And/or only edit the site content, perhaps tweaking html vs writing cfml code?

 

I ask because while you certainly COULD solve this problem via cfml in Application.cfc, if you're a developer I'd expect you'd just code it up. If you're not a developer, you maybe asking one of us to code it up. Someone else may offer that. I tend not to care to, as there are often other considerations not mentioned so it could be a lot of back and forth. In my opinion it's really not effective to use these forums for that sort of back and forth development of a coding solution.

 

But perhaps you're simply asking what folks think of your idea: it could work.

 

Really, though, this IS a problem typically better-solved via web server rewrite/redirect solutions. You mention htaccess. Some would argue you'd be better off figuring how to solve this there...but of course this is not the best place to ask about that. Some here may have that specific experience. We'll see.

 

But you could ask the question of a community related to htaccess, or the apache web server I assume you're using. No need to mention cf there, and thankfully your examples have no cfm files. You may find it's quite easy to get what you want.

 

But sure, some people would prefer to "just code the solution in cfml", and fair enough. But that then leads back to my first couple of questions. 🙂 You can help us help you.

 

Or again someone else may just offer some code for you based only on what you asked. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 11, 2024 May 11, 2024

Copy link to clipboard

Copied

Hello. I am a programmer but CF is almost unknown to me and so confusing. Changing html is not what I want because users maybe have bookmarked URL that points to wrong URL (404 page redirection happening). What should happen is not to force redirect, but move them to a page where it does exist. 

 

Currently, 404 redirection is done by IS in admin panel. So I am unsure how can I achieve this if that might enforce things before I manage to do anything here. 

If in Admin Panel, how can I redirect any URL as mentioned above? 

It's not like I need a ready code, but at least closer understanding of a problem. ChatGPT just didn't help with anything. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 11, 2024 May 11, 2024

Copy link to clipboard

Copied

I wasn't proposing you change any html. I was asking if you might be a site owner who is not a coder. You're now saying you're a coder but are not familiar with coldfusion. I assume you've either been given responsibility to manage a cf site, or you inherited or acquired the site or business whose site is running on it. I appreciate the "hard place" this puts you in.

 

It also helps explain many of your questions, I think. And we could  hope it may help others better help you, but sadly few seeing your other questions in the forums here (past or future) will ever see this and discover that. Oh well.

 

And now you're saying you use iis rather than apache, but you'd mentioned htaccess.

 

Let me just say that rather than seek the solution in htaccess (or cfml), note that iis has a free url rewrite module. It can be downloaded from Microsoft (and is NOT built into iis). But since you use iis, I'll note that you will find FAR MORE resources to help (or answers from chatgpt or elsewhere) on how to solve this VERY common problem you raise using this iis url rewrite feature.

 

(All that said, if you've already got heavy investment into using htaccess instead, then you may not want to switch to using the iis url rewrite feature.)

 

Whatever solution you pursue, one approach (to handle all the url/folder variants you may have) would be to use regular expressions. All rewrite tools offer that. Then again, learning how to use regex is another whole topic--but since you say you're a programmer, perhaps you already have that experience, or can figure it out. (Then again, even experienced programmers often get tripped up using regex.) 

 

My point is that such rewrite solutions absolutely could handle parsing that one kind of url to turn it into another, and yes carrying the ending part for all urls that follow that pattern, as you asked originally. 

 

Again, what you seek is something that ANY url rewrite feature can do: it's their very purpose. You just need to pick the right one for you and find how to use it to solve this very common.

 

Or as you've asked about here, you could punt and instead handle it in cfml. In that case there's just a variety of functions and variables that could investigate the incoming url, create a new location, and use cflocation (the tag or in script) to send the user that new url.

 

There are also url rewrite features in the underlying tomcat beneath cf, but some work only if using the built-in web server for cf, rather than when the request is passed to cf via iis or apache. 

 

I realize I'm saying a lot of words, versus just giving you "the answer". It goes back to my first response: there is no single "right" answer. There are options to consider, with pros and cons. Often people reply with just the simplest answer, even if it may not be best for your situation. That's why I'm asking questions and offering perspective, to help both you and others who may find this discussion in the future. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 20, 2024 May 20, 2024

Copy link to clipboard

Copied

LATEST

@Aleksandar34715023f1z9 , problem solved?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 12, 2024 May 12, 2024

Copy link to clipboard

Copied

quote

... Is it possible to do this in Application.cfc ...?


By @Aleksandar34715023f1z9

 

Yes, it is possible to do it in Application.cfc. 

The relevant Application.cfc is the one situated in the directory /Aion.

 

There are three cases to consider:

  1.  For every CFM file in /Computer/Games/Aion/ (new location), there is a CFM of the same name in /Aion (old location).
    You could then do the redirection in onRequestStart, as follows:
    <cffunction name="onRequestStart">
    	<cfargument name = "targetPage" type="String" required="true">
    	<!--- Determines whether to redirect request in the case of a currently existing CFM page --->
    	<cfset var toBeRedirected = false>
     	<cfset toBeRedirected = findNoCase("Computer/Games/Aion", arguments.targetPage) EQ 0>
     	<cfif toBeRedirected>
     		<cfset var newTargetPage = replaceNoCase(arguments.targetPage, "Aion", "Computer/Games/Aion", "one")>
     		<cflocation url="#newTargetPage#" statuscode="301" addtoken="no">	 		
     	</cfif>
    
    	<cfreturn true>
    </cffunction>​
  2. For each CFM file in /Computer/Games/Aion/ (new location) , there is no corresponding CFM file of the same name in /Aion (old location).
    You could then do the redirection in onMissingTemplate, as follows:
    <cffunction name="onMissingTemplate">
    	<cfargument name = "targetPage" type="String" required="true">
    	<!--- Determines whether to redirect request in the case of a currently non-existent CFM page --->
    	<cfset var toBeRedirected = false>
     	<cfset toBeRedirected = findNoCase("Computer/Games/Aion", arguments.targetPage) EQ 0>
     	<cfif toBeRedirected>
     		<cfset var newTargetPage = replaceNoCase(arguments.targetPage, "Aion", "Computer/Games/Aion", "one")>
     		<cflocation url="#newTargetPage#" statuscode="301" addtoken="no">	 		
     	</cfif>
    
    	<cfreturn true>
    </cffunction>​
  3.  For some CFM files in /Computer/Games/Aion/ (new location) , there are corresponding CFM files of the same name in /Aion (old location).  But there are CFM files in /Computer/Games/Aion/  for which there are no corresponding CFM files of the same name in /Aion. 
    You could then do the redirection in both event-handlers.:




     

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 12, 2024 May 12, 2024

Copy link to clipboard

Copied

We spider websites of new clients that we acquire before we convert their website to ColdFusion.  We do this to identify links that may be broken as a result of URL changes so that they don't suffer SEO-wise.

We use IIS and prefer using either a "rewriteMaps" web.config rule (w/301 permanent redirects) or writing IIS rewrite rules to take the full URL and pass it the path as a URL parameter for processing by CF. (This allows us to catch rules where dynamical values are being passed via the URL... be sure to add appendQueryString="true" to the action.)

Here are some articles regarding IIS rewrites/redirects.
https://stackoverflow.com/questions/37280888/iis-url-rewrite-subfolder-to-another-folder-name
https://www.rewriteguide.com/iis-redirect-directories-to-another-directory/

 

Here's an article on how to add all your rules to a separate file.  (NOTE: This has been our approach... we use ColdFusion to enable our clients to add their own rules and auto-generate the "rewritemaps.config" file.
https://ruslany.net/2010/05/storing-url-rewrite-mappings-in-a-separate-file/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 14, 2024 May 14, 2024

Copy link to clipboard

Copied

Hey @Aleksandar34715023f1z9 Did you find a solution yet?

For your specific need while hosting on IIS, you don't even need to use ColdFusion.  You can use a web.config IIS rewrite rule. (Just make sure you have the IIS Rewrite module installed already.)

Here's a sample web..config rule for permanently redirecting the root "Aion" directory:

<system.webServer>
  <!-- Other configuration settings... -->
  <rewrite>
    <rules>
      <rule name="Redirect Aion to New Path" stopProcessing="true">
        <match url="^Aion/(.*)" ignoreCase="true"/>
        <conditions>
          <add negate="true" input="{SCRIPT_FILENAME}" matchType="IsFile"/>
        </conditions>
        <action type="Redirect" url="/Computer/Games/Aion/{R:1}" appendQueryString="true" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>
  <!-- Other configuration settings... -->
</system.webServer>

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 15, 2024 May 15, 2024

Copy link to clipboard

Copied

@Aleksandar34715023f1z9 , you could follow in the footsteps of @James Moberg and generate a similar solution for Apache web server. To be implemented in .htaccess, of course. 

 

Alternatively, you may ask ChatGPT to suggest a solution for you. That is in fact what I did. The result follows:

 

 

BKBK (question to ChatGPT 4)

On my website I moved the folders to another location. What I now need is as follows: when a visitor requests https://bkbkguides.com/Aion/ , they should be redirected to https://bkbkguides.com/Computer/Games/Aion/ . An important consideration is that the solution should work for all folder changes. So, a request to https://bkbkguides.com/Aion/professions should be redirected to https://www.bkbkguides.com/Computer/Games/Aion/profession

 

ChatGPT (answer to BKBK question)

To handle the redirection you've described, especially when you want it to work for any folder changes dynamically, you can use the .htaccess file if your web server is running Apache. This file allows you to implement URL rewriting rules that automatically redirect visitors to the new location of the folders. Here's how you can set up the .htaccess file to achieve the redirection from any subdirectory within /Aion/ to its new path under /Computer/Games/Aion/.

 

Step 1: Create or Modify the .htaccess File

You need to create or modify the .htaccess file in the root directory of your website. If the file doesn't exist, you can create a new one.

Step 2: Add Redirection Rules

Here's an example of what to include in your .htaccess file to achieve the redirection:

RewriteEngine On
RewriteBase /
# Redirecting requests to /Aion/ to /Computer/Games/Aion/
RewriteRule ^Aion/(.*)$ https://www.bkbkguides.com/Computer/Games/Aion/$1 [R=301,L] 

Explanation of the Rules

  • RewriteEngine On: This line enables the rewriting capability.
  • RewriteBase /: This sets the base URL for per-directory rewrites.
  • RewriteRule: This is the directive used for defining a rewrite rule.
    • ^Aion/(.*)$: This pattern matches any URL that begins with /Aion/. The ^ symbol signifies the start of a URL, and (.*)$ captures everything after /Aion/ into a group (denoted by ()).
    • https://www.bkbkguides.com/Computer/Games/Aion/$1: This is the target URL where requests will be redirected. $1 is a placeholder that gets replaced by whatever was captured by (.*) in the pattern.
    • [R=301,L]: The flags at the end specify how the rule should be applied.
      • R=301 tells the server to issue a permanent redirect, helping search engines understand that the resource has moved permanently.
      • L indicates that this should be the last rule processed if this rule is matched.

Step 3: Upload or Save the .htaccess File

Make sure the .htaccess file is saved in the root directory of your website. If you're making changes to an existing file, ensure you take a backup before modifying it.

 

Test the Redirection

After you've implemented these changes, test the redirection by accessing URLs under the old /Aion/ path to see if they are correctly redirected to the new /Computer/Games/Aion/ path. It's important to check with different subpaths to ensure everything works as expected.

This setup should adequately handle the redirection needs for your website as described. If you have other directories besides /Aion/ that need similar redirection, you can add additional RewriteRule lines following the same pattern.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation