Forum Moderators: phranque

Message Too Old, No Replies

Another redirection questions

managed to redirect most but one

         

c5VamPirE

2:29 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



Hi guys, I have been reading this forum and getting a lot of information. There is however one problem that I am facing.

I am trying to redirect from
http://example.com/index.php?subaction=showcomments&id=1234
to the main page http://example.com

However any page with other than id=1234 should not be redirected. The reason is that this page is indexed in SEs but it is no longer available on my site. I have read posts here that suggested that one should redirect with code 301 instead of 404 not found and that actually makes sense as I ultimately want to capture the traffic and not send them away.

I was able to redirect
RewriteCond %{QUERY_STRING} &?=guideline[^&]*
RewriteRule ^$ http://example.com/? [R=301,L]

but can't get the former to work in a similar fashion. Can you please direct me to the right path?

Thanks in advance

Saied

jdMorgan

6:52 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Saied,

Can you post the code you tried to use? It would help us to discuss this problem.

Jim

c5VamPirE

7:33 am on Dec 28, 2006 (gmt 0)

10+ Year Member



Hi Jim,

I am still a novice but I made sure to read the forum charter and the mod-rewrite guide but wasn't able to find my answer

I tried a few things such as

RewriteCond %{QUERY_STRING} &?subaction=showcomments[^&]*
RewriteRule ^$ http://example.com/? [R=301,L]

and

RewriteCond %{QUERY_STRING} &index.php?subaction=showcomments[^&]*
RewriteRule ^$ http://example.com/? [R=301,L]

Without the index.php I am able to get it to work but with index.php in there i don't seem to be able to. Also adding the ID part is another problem so for example

RewriteCond %{QUERY_STRING} &?subaction=showcomments&ID=1234[^&]*
RewriteRule ^$ http://example.com/? [R=301,L]

doesn't work and it seems like I need two conditions perhaps? I hope it's clear now?

jdMorgan

7:47 am on Dec 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> two conditions, perhaps?

When you have both name/value pairs, are they always in this order?

subaction=showcomments&ID=1234

If their order can change, or if one or more additional name/value pairs might appear between them, then you'd need two conditions.

Jim

c5VamPirE

8:14 am on Dec 28, 2006 (gmt 0)

10+ Year Member



Hi there again, Well I have managed to do what I want but I thought I will post it here just to double check that I am doing it correctly

rewritecond %{QUERY_string} &?subaction=showcomments&id=1234&?
RewriteRule ^index\.php$ http://example.com/? [R=301,L]

Does that sound about right? It does do what I want

jdMorgan

8:30 am on Dec 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it appears that the main problem was your use of the pattern "[^&]*" at the end of your pattern, meaning, "Match as many characters (including zero) as you like, as long as none of them are ampersands."

If an ampersand with or without another name/value pair followed "id=1234", then your condition would always fail -- for example, "id=1234&user=bob" would not match, because the character after "4" *is* an ampersand.

The "&?" bounding the name/value pairs simply means, "If there is a character before or after my name/value pairs, it must be an ampersand." This is useful in case you may have other name/value pairs whose beginnings or ends match the desired string, but are longer.

For example, using the pattern "id=1234&?" prevents a match if id=12345. Or perhaps you might have another name/value called "oldsubaction" for backwards compatibility. Without the leading "&?", then a query of "oldsubaction=showcomments&ID=1234" would match the pattern "subaction=showcomments&ID=1234" unless it was start-anchored.

The regular expressions tutorial cited in our charter should make this clear if I haven't managed to do so.

Jim

c5VamPirE

8:49 am on Dec 28, 2006 (gmt 0)

10+ Year Member



Wow Jim excellent reply and thanks for the extra effort in explaining this to me. So as I understand it, the way I have it setup now with the query being between two &?

oldsubaction=showcomments&id=1234

is not a match neither is

subaction=showcomments&id=12345

I guess I could try it instead of asking :P

I have another question but I will attempt at solving it first before asking questions I think that works best so expect me to come back here in case I failed :P

Thanks Jim once again for your extra effort and great answer