redirect with cf

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
penelope112358Asked:
Who is Participating?
 
iamariConnect With a Mentor Commented:
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
 
srikanthmadishettiCommented:
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
 
srikanthmadishettiCommented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
iamariCommented:
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
 
iamariCommented:
actually, the cgi.script_name will include the forward slash / you should build the cfcase like:

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

etc.
0
 
srikanthmadishettiCommented:
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
 
srikanthmadishettiCommented:
CGI.SCRIPT_NAME will give every thign after server name excluding the url variables .
0
 
penelope112358Author Commented:
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
 
srikanthmadishettiCommented:
print CGI.SCRIPT_NAME   and see what it prints
0
 
penelope112358Author Commented:
is there a cf tag for printing the CGI.SCRIPT_NAME
0
 
srikanthmadishettiCommented:
<cfset thisScript = CGI.SCRIPT_NAME>

after this statement add this code
<cfoutput>
#thisScript#
</cfoutput>
0
 
penelope112358Author Commented:
<cfset thisScript = CGI.SCRIPT_NAME>

<cfoutput>
#thisScript#
</cfoutput>

returns /default.cfm

0
 
srikanthmadishettiCommented:
what is the url  you are  typing in the browser.
0
 
penelope112358Author Commented:
I am typing this into my browser: mydomainname.com or www.mydomainname.com.

No http,  and not mydomainname.com/default.dfm
0
 
iamariCommented:
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
 
srikanthmadishettiCommented:
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
 
iamariCommented:
the same goes for the other variable, "/Name1", it should be an actual file name the user will type
0
 
penelope112358Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.