Forum Moderators: phranque

Message Too Old, No Replies

Redirect 301 Help

         

Knucklehead00

4:23 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



Hey all.

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!

Knucklehead00

4:32 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



Did a bit more research into it, but still not working.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^FeatureID=$ [NC]
RewriteRule [(myurl)$...] [L,R=301]

g1smd

4:45 pm on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



More research is needed as you're guessing code.

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.

jd01

4:50 pm on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will help you more than I have time to type in a single post...

[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.)

Knucklehead00

5:13 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



I am not too sure if this is going to make a difference, but I am trying to put this redirect into the Drupal default htaccess which already has a load of other directives. I have a feeling that my condition/rule might not even be being looked at.

jd01

5:25 pm on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you have there will not ever match anything, please have a look at the resource I cited above and get closer on the code, then we can help you tweak it a bit to work with what you have... http:// is not ever present on the path request.

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.)

Knucklehead00

5:31 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



Alright. Clarification question on Point 6.

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.

jd01

5:42 pm on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to use a regular expression.
It's covered in the next post down in the library.

I posted them, because they are FAQs and I got tired of typing the answer over and over again ;)

[webmasterworld.com...]

Knucklehead00

5:57 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



Well, I cannot make sense out of any of this. Thanks for the trying.

jdMorgan

6:44 pm on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is why we use regular-expressions to match "patterns" and not fixed strings. There is a regular-expressions primer cited in our Apache Forum Charter, and I commend it to you.

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

Knucklehead00

3:03 pm on Oct 1, 2009 (gmt 0)

10+ Year Member



Nevermind stupid solution. Back to the drawing board.

Knucklehead00

5:37 pm on Oct 1, 2009 (gmt 0)

10+ Year Member



Alright, I think I am getting closer, but still no luck here.

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 :)

jdMorgan

6:12 pm on Oct 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope you're not linking to "index.cfm" instead of just declaring that file as your DirectoryIndex and linking to "/", but if you are, the following will work for links to either -- not in itself a good idea, but may help you along in your testing...

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]

However, this only works if you have a physically-existing page on your server at the filepath /"test/363" and if the URL you are typing-in or the link your are clicking on is "http://www.example.com/fuseaction=Feature\.showFeature&FeatureID=363." Since that's unlikely, I suspect you've got the whole process backwards.

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]

Mod_rewrite takes action as the requested URL is received from the client (browser or search robot), and (when using internal rewrite syntax) rewrites (changes) the request for the URL-path on the left into a request for the local filepath on the right, which must (generally) physically exist.

See Changing Dynamic URLs to Static URLs [webmasterworld.com] and other related threads in our Apache Forum Library for more information.

Jim