Forum Moderators: phranque
I have heard that this is the best solution to my current situation, but heck if I can figure out how to implement it properly.
What I want to do is a mod rewrite match 301 redirect.
Here is the HTAccess code that I have at the moment:
RewriteCond %{FeatureID} ^string1$ [NC]
RewriteRule (my site url)\string2 [L,R=301]
What I want to happen is that if the "FeatureID" is found on the URL, to take the FeatureID value and give it to the RewriteRule.
Such as:
index.cfm?fuseaction=Feature.showFeature&FeatureID=363
to
/363
Any ideas what I am doing wrong?
Thanks!
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^FeatureID=$ [NC]
RewriteRule [(myurl)$...] [L,R=301]
You need to capture data from the query string and re-use the %1 back reference so created.
You need a 'pattern' as well as a 'target' in your RewriteRule. The $ should be removed.
There's a lot of previous examples here. It's important for you to find out why the code doesn't work, so that you can maintain it in the future.
[webmasterworld.com...]
It's in the library with quite a few other resources.
(The link is on the top left of the Apache Forum Index.)
The only way any of us can really do anything to help is if you actually understand what's going on and how it works, because every situation is different and you are going to have to be able to interpret what we say and implement it to work with what you have.
(You could probably skim to Point 6 and get closer, but it's better if you take the time to read the whole thing if you really want it to work.)
If the FeatureID=(whatever value) is always changing and never static, how can cover that? It seems that you are coding for it being static.
IE -- (var1) is the value that will get placed is $1. What happens if that (var1) value is not consistent? I would think that what you pull from the RewriteCond Query would somehow filter down into the RewriteRule (maybe being able to pull it), so that you know what value is being appended to the target.
I posted them, because they are FAQs and I got tired of typing the answer over and over again ;)
[webmasterworld.com...]
Take JD01's warning to heart. It seems you're in a big hurry to get 'just a little code solution' here. But if you don't understand every last little detail of that 'little code solution' you might well blow up your server (if you are lucky), or if not, leave a tiny error in the code that might slowly destroy your search engine rankings over the next year. If you don't understand the code (or hire an expert to come in and look at it for you), you might well be out of business before you find the problem... or even know that there *is* a problem.
In RewriteConds and RewriteRules, we match request-related strings against patterns. If we need to re-use those matched strings (or pieces or parts of them), we enclose the sub-pattern(s) used to match them in parentheses, and then use $1-$9 and %1-%9 to back-reference their values -- as described in the mod_rewrite documentation at Apache.org (also cited in our forum charter).
Jim
I took the rewrite rule and put it in a different htaccess in a different folder so as to test what is going on and not having other rules interfering for the time being.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index\.cfm?fuseaction=Feature\.showFeature&FeatureID=([0-9]+)$ /test/$1
I have read through the entire first 5 pages of this forum and this is close as I am getting to the solution. So, if the string in the pattern matches with some number or group of numbers at the end, those groups of numbers are assigned to a variable. Then on the right side, we put together the new writing path and give $1 the value of the number that was in the parentheses on the left side via back-referencing.
If I am getting warmer or colder, would be great to know :)
RewriteRule looks only at the URL-path and cannot 'see' query strings. You must test them separately using a RewriteCond, and back-reference captured values using %1-%9 in the RewriteRule substitution:
Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^fuseaction=Feature\.showFeature&FeatureID=([0-9]+)$
RewriteRule ^(index\.cfm)?$ /test/%1 [L]
Link to "/test/363" in the HTML <a href=""> links on your pages (modify your script or database to do this). Then use mod_rewrite to 'connect' that 'new/friendly/static-looking' URL to your script:
RewriteRule ^test/[0-9]+$ /index.cfm?useaction=Feature\.showFeature&FeatureID=$1 [L]
See Changing Dynamic URLs to Static URLs [webmasterworld.com] and other related threads in our Apache Forum Library for more information.
Jim