Forum Moderators: phranque

Message Too Old, No Replies

Dynamic CF URL redirect to Static HTML

man, I thought this was easy...

         

micahb37

11:04 pm on Jun 15, 2006 (gmt 0)

10+ Year Member



I am trying to redirect www.domain.com/public/index.cfm?catID=# to www.domain.com/public-html/somepage.html

I am trying to do it through the .htaccess file, but have spent quite awhile trying ReDirect rules, mod_rewrite rules, etc, but I cant seem to get a dynamic page to redirect to a static one.

Any help is appreciated.

micah

LifeinAsia

11:44 pm on Jun 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Just usa a cflocation tag in index.cfm.

jdMorgan

3:39 am on Jun 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is more usual to link to static-appearing URLs and then internally rewrite requests for those URLs to the script that generates your dynamic content.

In other words, if you are trying to achieve 'search-engine-friendly' URLs, you're going about it backwards.

Otherwise, be aware that a query string is not part of a URL, but rather data appended to a URL to be passed to the resource at that URL. For this reason, in addition to RewriteRule, you'll need to use RewriteCond %{QUERY_STRING} in mod_rewrite to test and handle query strings.

Jim

micahb37

3:54 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



OK, so here is what I came up with in my htaccess:

# Redirect of old pages to new
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{QUERY_STRING} ^catID=2$
RewriteRule ^$ /public-html/company.html [L]
RewriteCond %{QUERY_STRING} ^catID=3$
RewriteRule ^$ /public-html/services.html [L]
RewriteCond %{QUERY_STRING} ^catID=4$
RewriteRule ^$ /public-html/product.html [L]
RewriteCond %{QUERY_STRING} ^catID=6$
RewriteRule ^$ /public-html/clientlist.html [L]
RewriteCond %{QUERY_STRING} ^catID=7$
RewriteRule ^$ /public-html/contactus.html [L]

It still isnt working. I didnt place the code in the cfm page. Could it be because I am trying to redirect pages in the public folder to a public-html folder?

LifeinAsia

4:23 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Rewrite is usually used to go from static URL to dynamic page, not the other way around.

As I originally explained, just use cflocation in your index.cfm file. There is no need to muck around with the .htaccess file. It is *VERY* simple- put the following code in your index.cfm file:
<cfif catid EQ 2>
<cflocation url="/public-html/company.html">
<cfelseif catid EQ 3>
<cflocation url="/public-html/services.html">
...
</cfif>

jdMorgan

4:25 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The syntax of your code specifies an internal rewrite, not a redirect.

The proper way to *redirect* from www.example.com/?catID=$2 to www.example.com/company.html would be:


RewriteCond %{QUERY_STRING} ^catID=2$
RewriteRule ^$ http://www.example.com/public-html/company.html [R=301,L]

Also, I assume that your pages link to "/catID=2" and that you want to redirect that to public-html/company.html instead. In other words, your old site was dynamic, and you are now changing it to use static pages. If not, please re-read my post above, and review this library post [webmasterworld.com].

There are also more efficient ways to do 'associative lookups,' but we'll get to that after you have achieved basic functionality.

The code has to be placed in the directory-path that is requested by the linked-to URL. If you link to www.example.com/public/index.cfm?catID=#, then the .htaccess code will need to be in the /public directory. If "/public" does not appear in the link, then you're probably dealing with a more complex issue, and more info will be needed.

Jim

micahb37

4:48 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



LifeInAsia:

Quick question. If I were to us the cflocation tag, would that make both URLs exist? I am trying to move a old site structure to a new site structure using 301 redirects of the old pages.

JDMorgan:

I am trying to move a dynamic site to a static site, but want to 301 redirect the old pages to the new.

So, I made modifications to my .htaccess file:


# Redirect of old pages to new
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^catID=2$
RewriteRule ^$ http://www.example.com/public-html/company.html [R=301,L]
RewriteCond %{QUERY_STRING} ^catID=3$
RewriteRule ^$ http://www.example.com/public-html/services.html [R=301,L]
RewriteCond %{QUERY_STRING} ^catID=4$
RewriteRule ^$ http://www.example.com/public-html/product.html [R=301,L]

But still no love. Its in the /public folder. One question, your example indicates a URL of www.example.com/?catID=#. In truth, the URL is www.example.com/index2.cfm?catID=#. I was under the impression that the RewriteCond %{QUERY_STRING} dealt only with catID, and didnt care what the file name is. Am I off?

micah

LifeinAsia

4:52 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Add the following above each cflocation tag (where "thislink" is the link you're redirecting to):
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="ThisLink">

jdMorgan

4:59 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're not off on RewriteCond, but the RewriteRule pattern requires a URL of "/", since your current pattern is "^$" (mod_rewrite in .htaccess strips the leading slash.) You'll need to change the rule pattern to accept "index2.cfm" if that's the page you're trying to redirect from:

RewriteCond %{QUERY_STRING} ^catID=2$
RewriteRule ^index2\.cfm$ http://www.example.com/public-html/company.html [R=301,L]

Jim

micahb37

5:06 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



So two questions.

1) Using a cflocation tag creates a client side redirect versus server side, correct?

2) Knowing nothing about cold fusion, is this the proper context:

[code]
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="http://www.example/com/public-html/company.html">
<cflocation ="http://www.example.com/public/index.cfm?catID=2">
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="http://www.example/com/public-html/company.html">
<cflocation ="http://www.example.com/public/index.cfm?catID=3">

Thanks for the help.

micah

LifeinAsia

5:22 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



1) No, ColdFusion is all done on the server side.

2) No, you need the <cfif> structure I posted in message 5. What you just wrote will put to in an infinite loop that will eventually crash Cold Fusion.

[code]
<cfif catid EQ 2>
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="/public-html/company.html">
<cflocation url="/public-html/company.html">
<cfelseif catid EQ 3>
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="/public-html/services.html">
<cflocation url="/public-html/services.html">
...
</cfif>

micahb37

5:23 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



jdMorgan:

Ok, it all is making sense. I have gotten it to redirect to the right URLs, except the catID is being appended to the end of the correct URL.

so www.example.com/public/index2.cfm?catID=3 redirects to www.example.com/public-html/products.htmlcatID=3

Otherwise, it works like a charm. Thank you for the walkthrough, mod_rewrite can be rather daunting.

micah

micahb37

5:46 pm on Jun 21, 2006 (gmt 0)

10+ Year Member



LifeInAsia:

So, it seems that I got it to work, except for a weird deal.

the Server Status Code returned is: 302, Moved Permanently versus 301

Here is the code:


<cfif catid is 2>
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="/public-html/company.html">
<cflocation url="/public-html/company.html">

<cfelseif catid is 3>
<cfheader statuscode="301" statustext="Moved permanently">
<cfheader name="Location" value="/public-html/services.html">
<cflocation url="/public-html/services.html">

....
</cfif>

should I use #url.catid# versus just catid? Does case matter?

micah

jdMorgan

5:53 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, here's some more 'daunt' for you. :)

This uses a little mod_rewrite trick to create associations between lexically-unrelated strings, and allows the use of a single RewriteRule instead of a whole stack of them:


RewriteCond %{QUERY_STRING}<->company ^catID=2<->(.+)$ [OR]
RewriteCond %{QUERY_STRING}<->services ^catID=3<->(.+)$ [OR]
RewriteCond %{QUERY_STRING}<->product ^catID=4<->(.+)$
RewriteRule ^index2\.cfm$ http://www.example.com/public-html/%1.ht[b]ml?[/b] [R=301,L]

The trailing "?" in the RewriteRule substitution will clear the query string problem you report.

Also note that the last RewriteCond *must not* have an [OR] flag on it.

The "<->" string in the code above is arbitrary; It is used only to allow the query_string/category_name string to be parsed out by serving as a unique demarcation point indicator in the patterns. Although I use it to 'remind' myself of using a concatenated string, it has no special meaning to the regular-expressions or mod_rewrite parsers; You could use any character or character-sequence that you are sure will never occur in your query string.

Jim

LifeinAsia

5:55 pm on Jun 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Hmmm. Try entering the full URL (e.g., "http://www.domain.com/public-html/company.html" instead of "/public-html/company.html") in the cfheader tag. I don't see why it should matter though.

Yes, probably better programming practice to scope the variable with url.catid but it shouldn't make any difference in your case.

Case doesn't matter.

micahb37

4:55 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



jdMorgan:

Works like a charm, and it really helped to walk through it. Thank you.

LifeInAsia:

Tried the full URL, still getting the 302 issue (it was elevated by utilizing the mod_rewrite stuff). I would like to figure it out. Any thoughts?

micah

LifeinAsia

4:56 pm on Jun 22, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Doing both the CF and rewrite may cause it. I'd say pick one or the other, but not both.

micahb37

5:23 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



I tried each individually. I wanted to learn how both worked.