Forum Moderators: phranque

Message Too Old, No Replies

Writing a 301 need some help.

         

nickCR

2:15 am on Jan 11, 2012 (gmt 0)

10+ Year Member



Hello All

Trying to find a way to rewrite these urls

http://www.example.com/index.php?act=calendar&cal_id=1&code=showweek&week=1308458973

http://www.example.com/index.php?autocom=casino&do=stats&gameid=5&st=300

to


RewriteCond %{QUERY_STRING} ^(act=calendar|autocom=casino)$ [NC]
RewriteRule (.*) http://www.mydomain.com [R=301,L]


Thanks in advance for your help.

[edited by: nickCR at 3:00 am (utc) on Jan 11, 2012]

lucy24

2:51 am on Jan 11, 2012 (gmt 0)

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



Do a Forums search for "Query String". You are on the right track by looking at the query in a RewriteCond. But note that your anchors mean that the Condition will only match if the query contains nothing but one of the two specified forms. You will also need to read up on how to capture and change existing queries.

As currently written, your RewriteRule redirects all requests-- including those that already ask for the home page-- to the home page. This typically results in the user's browser (not the server) putting its foot down and saying "This is going nowhere fast".

Finally, there's a reason we insist on www.example.com. Try it with your next post and you will see.

nickCR

3:33 am on Jan 11, 2012 (gmt 0)

10+ Year Member



I tried this but it's not working.

It's redirecting but it's just removing the index.php from the query string and rewriting the url.

http://www.example.com/index.php?act=calendar&osr=123&123=adsa

turns into:

http://www.example.com/?act=calendar&osr=123&123=adsa

RewriteCond %{QUERY_STRING} ^.*act=calendar.*$
RewriteRule . http://www.example.com/ [R=301,L]

RewriteCond %{QUERY_STRING} ^.*autocom=casino.*$
RewriteRule . http://www.example.com/ [R=301,L]


Thanks for the tip on the example.com, works great :)

lucy24

4:26 am on Jan 11, 2012 (gmt 0)

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



it's just removing the index.php from the query string and rewriting the url

You don't mean "query string", you mean "request". I hope it was just a typo.

You don't need the anchors and .* btw. This
^.*act=calendar.*$
is exactly the same as this
act=calendar

Anyway, it looks as if your RewriteRule is working perfectly. Your pair of rules-and-conditions say: If the query string contains one or the other of these elements, redirect the request to the front page without changing the query string.

Chorus:
I really hate this damned machine
I wish that they would sell it
It never does just what I want
But only what I tell it.


And now go do that Forums search for "query string". You will emerge wiser. Honest. :)

g1smd

4:55 am on Jan 11, 2012 (gmt 0)

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



Never use .* at the beginning of a subpattern.

Using uncaptured .* at the end of a subpattern is redundant.

What you actually need is (^|&) and (&|$) here.

Clear the query string to prevent an infinite loop. A forum search will find hundreds of threads with that answer and further useful information.

nickCR

5:06 am on Jan 11, 2012 (gmt 0)

10+ Year Member



This is working perfectly for me.

Thanks for your help :), if you have any suggestions let me know :)


RewriteCond %{QUERY_STRING} (act=calendar|autocom=casino)
RewriteRule ^.*$ http://www.example.com/? [R=301,L]


This works too.


RewriteCond %{QUERY_STRING} (act=calendar|autocom=casino)
RewriteRule ^index.php$ http://www.example.com/? [R=301,L]

g1smd

5:23 am on Jan 11, 2012 (gmt 0)

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



You probably do need the anchoring for query strings.

^ and $ are redundant with .*


With index.php you need to escape the literal period.

nickCR

5:26 am on Jan 11, 2012 (gmt 0)

10+ Year Member



Like this then:


RewriteCond %{QUERY_STRING} (act=calendar|autocom=casino)
RewriteRule ^index\.php$ http://www.example.com/? [R=301,L]


Also this is restricted to the index.php which I believe is good.

lucy24

8:27 am on Jan 11, 2012 (gmt 0)

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



This works too.

If index.php is the only file that ever has a query string, then there wouldn't be any visible difference. But it's a good idea to constrain your rule as narrowly as possible. Otherwise apache has to stop and evaluate the Condition for every single request it receives-- not just for irrelevant pages, but even for things like images or style sheets.

Does the query string always begin with one of your two options? That is, assuming they're present at all. If so, you can save a few nanoseconds by putting in a beginning anchor ^. That way mod_rewrite doesn't have to read the entire query on the off chance that "act=" or "autocom=" will show up later.

All this is of course assuming that your aim is to dump the entire query string if it contains one of those two items.

g1smd

8:48 am on Jan 11, 2012 (gmt 0)

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



To put that question differently, "are there any query strings that you want to retain?"

nickCR

5:59 pm on Jan 11, 2012 (gmt 0)

10+ Year Member



No it's just those 2 and actually one more because it's an old plugin for the forum that has been disabled. So those pages are showing up as 404's.

g1smd

7:39 pm on Jan 11, 2012 (gmt 0)

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



No it's just those two


Ambiguous answer. It's just those two that what?

g1smd

7:39 pm on Jan 11, 2012 (gmt 0)

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



del

[edited by: g1smd at 7:52 pm (utc) on Jan 11, 2012]

nickCR

7:50 pm on Jan 11, 2012 (gmt 0)

10+ Year Member



Sorry for not being clear. That what happens when your in a rush.

There are currently only 3 urls that consistently show up in the 404 report on GWT and those are the 3 that I am correcting with this re-write. I am not sure that the query will always maintain the exact same structure so for that reason I am not going to add the "^" until I'm 100% sure that nothing else is popping up.

g1smd

7:51 pm on Jan 11, 2012 (gmt 0)

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



OK. You have three you want to correct. I get that.

Are there any other query strings used by the site that you want to retain? If not, there's a simpler pattern you could use to correct all requested query strings.

lucy24

11:54 pm on Jan 11, 2012 (gmt 0)

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



Let's put the question a different way. There are two pieces:

The individual query, which goes (^|&)([^&]+)

The whole query string, which is made up of all your individual queries strung together.

When you look at %{QUERY_STRING} it ignores the leading ? so you will have either ^ for the very first query, or & for subsequent queries.

So the question is: do you want to delete only those two specific queries-- or do you want to delete any query string that contains either of those queries?


###, Forums, why would a close-parenthesis by itself turn into a wink?