Solved

redirect with cf

Posted on 2010-08-26
18
427 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
<cfquery> to Excel 5 56
Coldfusion get content of HTML table 8 147
On Submit return to same spot 7 85
Passing value to a stored procedure 8 93
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

861 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

26 Experts available now in Live!

Get 1:1 Help Now