Forum Moderators: phranque
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
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
# 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?
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>
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]
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
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
RewriteCond %{QUERY_STRING} ^catID=2$
RewriteRule ^index2\.cfm$ http://www.example.com/public-html/company.html [R=301,L]
Jim
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
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>
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
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
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]
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
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.