Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

redirect with cf

Posted on 2010-08-26
18
Medium Priority
?
439 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
ID: 33535850
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
ID: 33535995
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
ID: 33536790
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
LVL 2

Expert Comment

by:iamari
ID: 33536899
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
ID: 33541571
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
ID: 33541577
CGI.SCRIPT_NAME will give every thign after server name excluding the url variables .
0
 

Author Comment

by:penelope112358
ID: 33561666
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
ID: 33561760
print CGI.SCRIPT_NAME   and see what it prints
0
 

Author Comment

by:penelope112358
ID: 33561810
is there a cf tag for printing the CGI.SCRIPT_NAME
0
 
LVL 13

Expert Comment

by:srikanthmadishetti
ID: 33561820
<cfset thisScript = CGI.SCRIPT_NAME>

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

Author Comment

by:penelope112358
ID: 33561966
<cfset thisScript = CGI.SCRIPT_NAME>

<cfoutput>
#thisScript#
</cfoutput>

returns /default.cfm

0
 
LVL 13

Expert Comment

by:srikanthmadishetti
ID: 33562126
what is the url  you are  typing in the browser.
0
 

Author Comment

by:penelope112358
ID: 33562215
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
ID: 33562245
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
ID: 33562298
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
ID: 33562419
the same goes for the other variable, "/Name1", it should be an actual file name the user will type
0
 

Author Comment

by:penelope112358
ID: 33565089
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 2000 total points
ID: 33591526
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month21 days, 1 hour left to enroll

810 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