Solved

redirect with cf

Posted on 2010-08-26
18
426 Views
Last Modified: 2012-05-10
Hello,

my webserver data are as follows:

ColdFusion 5
Windows Server 2003
IIS 6.1.

I would like to do a redirect.

Specification for the URL field:

1. If a user enters XXXX.com the user will be pointed to:
http://XXXX.com/MainPath/Main.html

2. If a user enters www.XXXX.com the user will be pointed to:
http://www.XXXX.com/MainPath/Main.html

3. If a user enters XXXX.com/name1 the user will be pointed to http://XXXX.com/Path1

4. If a user enters XXX.com/name2 the user will be pointed to http://XXXX.com/Path2

5. If a user enters XXX.com/name3 the user will be pointed to http://XXXX.com/Path3

I was thinking of a cfloop or something similar but can't get redirects 3. to 5. to work.
 
Thanks, Penelope
0
Comment
Question by:penelope112358
  • 8
  • 5
  • 5
18 Comments
 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
First try to get the current url

<cfset currrenturl =   "http://" & cgi.server_name & cgi.script_name &  "?" & cgi.query_string>

<cfif listfind(currrenturl ,"name1","/")>
 <cfset urltoredirect =  "http://" & cgi.server_name &Path1>
</cfif>
<cfif listfind(currrenturl ,"name2","/")>
 <cfset urltoredirect =  "http://" & cgi.server_name &Path2>
</cfif>
<cfif listfind(currrenturl ,"name3","/")>
 <cfset urltoredirect =  "http://" & cgi.server_name &Path3>
</cfif>
<cfif urltoredirect neq "">
<cflocation url = "#urltoredirect #">
</cfif>

I don't understand why you need 1st and 2nd use case but try to use the above code for 3 to 5

0
 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
path1 and path2 are variables which has the folder and file path details like

<cfset path1 = "/test/index.cfm">
<cfset path2 = "/test2/index.cfm"> ....
0
 
LVL 2

Expert Comment

by:iamari
Comment Utility
you can try something like this:

<cfset thisScript = CGI.SCRIPT_NAME>

<cfif thisScript neq ''>

      <cfswitch expression=#thisScript#>
            <cfcase value="Name1">
                  <cfset thisURL="http://www.XXXX.com/Path1">            
            </cfcase>
            <cfcase value="Name2">
                  <cfset thisURL="http://www.XXXX.com/Path2">            
            </cfcase>
            <cfdefaultcase>
                  <cfset thisURL="http://www.XXXX.com/">
            </cfdefaultcase>
      </cfswitch>

      <cflocation url="#thisURL#">
</cfif>
0
 
LVL 2

Expert Comment

by:iamari
Comment Utility
actually, the cgi.script_name will include the forward slash / you should build the cfcase like:

 <cfcase value="/Name1">
....
 <cfcase value="/Name2">

etc.
0
 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
CGI.SCRIPT_NAME will give evry thign after domain name so it's better to find the name1 in that string rather than an eq what if some one types
XXXX.com/name1/index.cfm  so it is always better to find name1 instead make eq comparision.
0
 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
CGI.SCRIPT_NAME will give every thign after server name excluding the url variables .
0
 

Author Comment

by:penelope112358
Comment Utility
Hello,

I inserted the slash

<cfset thisScript = CGI.SCRIPT_NAME>

<cfif thisScript neq ''>

      <cfswitch expression=#thisScript#>
            <cfcase value="/Name1">
                  <cfset thisURL="http://www.XXXX.com/Path1">            
            </cfcase>
            <cfcase value="/Name2">
                  <cfset thisURL="http://www.XXXX.com/Path2">            
            </cfcase>
            <cfdefaultcase>
                  <cfset thisURL="http://www.XXXX.com/">
            </cfdefaultcase>
      </cfswitch>

      <cflocation url="#thisURL#">
</cfif>


but it won't do the redirect. Even the default case won't run.
0
 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
print CGI.SCRIPT_NAME   and see what it prints
0
 

Author Comment

by:penelope112358
Comment Utility
is there a cf tag for printing the CGI.SCRIPT_NAME
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
<cfset thisScript = CGI.SCRIPT_NAME>

after this statement add this code
<cfoutput>
#thisScript#
</cfoutput>
0
 

Author Comment

by:penelope112358
Comment Utility
<cfset thisScript = CGI.SCRIPT_NAME>

<cfoutput>
#thisScript#
</cfoutput>

returns /default.cfm

0
 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
what is the url  you are  typing in the browser.
0
 

Author Comment

by:penelope112358
Comment Utility
I am typing this into my browser: mydomainname.com or www.mydomainname.com.

No http,  and not mydomainname.com/default.dfm
0
 
LVL 2

Expert Comment

by:iamari
Comment Utility
penelope, you have to replace those URL values with real values, the ones your users will actually type.

obviously, "http://www.XXXX.com/Path1" won't go anywhere, you have to change them to something like

http://www.myDomainName.com/default.cfm
0
 
LVL 13

Expert Comment

by:srikanthmadishetti
Comment Utility
your  requirement is very confusing...  when you type  www.mydomainname.com. you will not get the cgi.script_name

when you say


3. If a user enters XXXX.com/name1 the user will be pointed to http://XXXX.com/Path1

4. If a user enters XXX.com/name2 the user will be pointed to http://XXXX.com/Path2

5. If a user enters XXX.com/name3 the user will be pointed to http://XXXX.com/Path3


what is your requirement in this .

when we gave you to use  cgi.script_name  we were pointing of 3 ,4,5 points,

0
 
LVL 2

Expert Comment

by:iamari
Comment Utility
the same goes for the other variable, "/Name1", it should be an actual file name the user will type
0
 

Author Comment

by:penelope112358
Comment Utility
Hello srikanthmadishetti, hello iamari,

 I have created a directory "testit" directly under the root directory. It only contains "default.cfm" and default.cfm contains

<cfset thisScript = CGI.SCRIPT_NAME>
<cfoutput>
#thisScript#
</cfoutput>

As expected, it does return /testit/default.cfm.

For my redirect requirement I am currently using the following solution:

I do have to create a subdirectory "name1" directly under the root directory. "name1" contains "index.cfm" , like this: /name1/index.cfm.  "index.cfm" contains

<CFLOCATION url="Path1"> where Path1 starts with a "/" and ends with a .cfm file. Path1 gets concatenated with "XXXX.com" and this works very well. If "XXXX.com/name1" is entered into the browsers' address bar, it will perform the redirect to "XXXX.com/Path1".

However, this solution means that I do have to create a direcorty "XXXX.com/name" for each redirect. And I am wonderng, if that can be avoided. So, if "XXXX.com/name1" is entered into the browser, a redirect to "http://XXXX.com/Path1" should happen without "name1" existing at all. But I guess, this may not be possible, because if "XXXX.com/name1" is entered into the browser, the first thing that will be looked for is the directory "name1" and not a script for redirecting.

I was hoping, that I could somehow get the server to first read the script, so I don't have to create all of those "name" directories and index.cfms they contain. But now I guess, the redirect from "XXXX.com/name1" to "XXXX.com/Path1" will never work, if "XXXX.com/name1" doesn't exist.

This, I think, is also what you, iamari, mean, when you say is should be an acutal file (directory?) the user will type?

I'd be interested in your comments on this.

Thanks, Penelope
0
 
LVL 2

Accepted Solution

by:
iamari earned 500 total points
Comment Utility
Penelope, i think i understand now better what you want. i guess your user will enter in his browser a url  like:

http://www.mydomain.com/index.cfm?uName=n1
or
http://www.mydomain.com/index.cfm?uName=n2
etc

This is the only way the browser can always point to the same page (your index.cfm page) but redirect the user to a different page (in the same directory or a different one) based on the URL variable the user is passing (what comes after the ? sign in the URL string)

i've created the following files:
- index.cfm file containing the script
- test1.cfm file in the root directory
- test2.cfm file in a subfolder named mySubDir
- a default file (if the user doesn't enter any variable) in the root directory

below is what your index.cfm file should look like
make sure you edit it and
- replace www.myDomain.com with your real URL
- replace the variable name and value (uName=n) with whatever you want to use
- replace the target file names/path (test1, test2) with your correct file names/path

<cfset thisScript = CGI.QUERY_STRING>

 <cfswitch expression=#thisScript#>
       <cfcase value="uName=1">
             <cfset thisURL="http://www.myDomain.com/test1.cfm">
       </cfcase>
       <cfcase value="uName=2">
             <cfset thisURL="http://www.myDomain.com/mySubDir/test2.cfm">            
       </cfcase>
       <cfdefaultcase>
             <cfset thisURL="http://www.myDomain.com/myDefaultPage.cfm">
       </cfdefaultcase>
 </cfswitch>

 <cflocation url="#thisURL#">

hope that helps more

regards
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now