Forum Moderators: phranque
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
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?
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
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
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