Forum Moderators: phranque

Message Too Old, No Replies

Redirect based on Query String

         

htmanning

10:06 pm on Feb 21, 2004 (gmt 0)

10+ Year Member



I'm trying to redirect based on part of a query string. I have a site linking to software.html on my server. I've tried several things includingthe following. All of them include RewriteEngine on

RewriteCond %{QUERY_STRING} \?(.*)=Test([^&]*)
RewriteRule ^software\.html$ [mydomain.com...] [L]

RewriteCond %{QUERY_STRING} ^.*PlatformName=Test.* [NC]
RewriteRule ^software\.html$ [mydomain.com...] [L]

and this:
RewriteCond %{QUERY_STRING} .*PlatformName=Test
RewriteRule ^software\.html$ [mydomain.com...] [L]

None of these work at all. How can I make this happen? The query string from the site is long...lots of stuff in there so I need the .htaccess file to filter out the garbage and just look for the word Test. If it finds Test, go to the site I want. Then, I want to be able to say if it finds Test2, go somewhere else.

jdMorgan

2:16 pm on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



htmanning,

Two of your three code snippets should have worked, although possibly not as expected. The first one will fail because "?" is not part of the QUERY_STRING. Neither is it part of the REQUEST_URI; it only serves to delimit the two, and is not stored into either variable.

You may need to add the directive Options +FollowSymLinks ahead of your RewriteEngine on directive.

It's not clear whether you have any other mod_rewrite code working yet or not, so it may be that your server does not support it at all. I suggest a simple test, such as the following to determine whether you can use mod_rewrite:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /software2.html [L]

With this code in place, request "silly.html" using your browser. Your server should return the contents of "software2.html" if mod_rewrite is working. If not, check your server error log file -- It often gives a very good clue as to what is wrong.

There are two basic forms of RewriteRules; internal rewrites and external redirects.

An internal rewrite based on your third example above would be:


RewriteCond %{QUERY_STRING} PlatformName=Test
RewriteRule ^software\.html$ /software2.html [L]

This simply serves /software2.html whenever software.html is requested with a query string containing "PlatformName=Test"

An external redirect based on the same example would be:


RewriteCond %{QUERY_STRING} PlatformName=Test
RewriteRule ^software\.html$ http://www.example.com/software2.html? [R=301,L]

This serves /software2.html from the specified (usually external) domain whenever software.html is requested with a query string containing "PlatformName=Test". It also clears the query string, and updates the user's browser address bar. Unlike internal rewrites, external redirects require the cooperation of the user's browser, and in fact, require the user's browser to re-request the resource from the new URL.

The code above is intended for use in .htaccess, since it appears that your original code was, too. For use in httpd.conf, prefix the pattern in the RewriteRules with "/", e.g. ^/software\.html$".

Jim

htmanning

7:23 pm on Feb 22, 2004 (gmt 0)

10+ Year Member



Thanks for the detailed response. It only sometimes, and it never works if I have another argument after it. I did the simple forwarding first as you said and it does work on my server.

This worked by itself:

RewriteCond %{QUERY_STRING} platformName=Test
RewriteRule ^software\.html$ [example.com...] [R=301,L]

but if I added another one to it, it doesn't work. For example:

RewriteCond %{QUERY_STRING} platformName=Test
RewriteRule ^software\.html$ [example.com...] [R=301,L]

RewriteCond %{QUERY_STRING} platformName=TestNext
RewriteRule ^software\.html$ [examplenext.com...] [R=301,L]

If I have more than one argument it doesn't work. Also, how do I ignore case? What if it's platformName=test, or platformname=Test, etc. I just realized that they use different cases in their urls.

Thanks.

jdMorgan

8:43 pm on Feb 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> it never works if I have another argument

Please define "never works" -- what happens -- or doesn't happen -- that you do or don't want to happen?

And note that I said, "It also clears the query string..." above. If that's not what you want, you;ll need to define what you want to happen to to original query string.

mod_rewrite can do almost anything you want, but the expected inputs and the desired outputs need to be well-defined before you start.

Add [NC] to your RewriteRule or RewriteCond to tell that directive to ignore case.

Long-term, get rid of the uppercase and mixed-casee urls and querystrings -- they cause a lot of problems because Apache and *nix are case-sensitive.

Jim

htmanning

4:37 am on Feb 23, 2004 (gmt 0)

10+ Year Member



I put the code that you recommended in my .htaccess file like the following:

RewriteCond %{QUERY_STRING} platformName=Test
RewriteRule ^software\.html$ [example.com...] [R=301,L]

RewriteCond %{QUERY_STRING} platformName=TestNext
RewriteRule ^software\.html$ [examplenext.com...] [R=301,L]

It works sometimes (about 10% of the time), other times it just goes to software.html. If I delete software.html I get an error because it's not there. It seems like the server will redirect a request once, but then it won't work again for awhile...maybe 10 minutes...maybe longer. I took everything out except for one entry...

RewriteCond %{QUERY_STRING} platformName=Test
RewriteRule ^software\.html$ [example.com...] [R=301,L]

and I still could not get it to work consistently. Sometimes it will redirect appropriately, other times it ends up on software.html.

As for clearing the query string, that's fine with me if it goes to the right redirect. Basically I have a several software.html files at different domains on the same server..

abc.com/software.html
def.com/software.html
ghi.com/software.html

These software.html files have meta tag redirects that happen and those are working correctly. The thing is I have a company that points to another one of my domains:

123.com/software.html

So, based on the query string, I need to take the user from 123.com/software.html to either abc.com, def.com, ghi.com but it depends on the query string I receive from the other company. Make sense? So, my .htaccess file is at 123.com and I'm trying to redirect based on what I receive. The query string can be cleared after I reroute the user and it's fine. It just seems like I'm not able to capture the query string all of the time. It only goes to the proper redirect on a limited basis, even if I change nothing.

Thanks.

jdMorgan

6:17 am on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry to have to keep asking questions, but it sounds like you do have a complex problem...

Are abc, def, ghi, and 123.com hosted separately, or are you sharing a server between any or all of them?

Jim

htmanning

7:07 am on Feb 23, 2004 (gmt 0)

10+ Year Member



They are all on the same server. I am trying to forward through the other domains though. Anyone coming to 123.com get's passed to abc.com or another domain, but they're all on the same server. The thing is, they don't even get that far. They just stay on software.html on 123.com. It's weird.

htmanning

11:16 am on Feb 23, 2004 (gmt 0)

10+ Year Member



Now, nothing works. I can get one redirect to work if I put! before platformname= but I think! cancels it out so anything in the query string will make it redirect.

jdMorgan

5:05 pm on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm stumped...

Do you have any other redirects that could be interfering?

Are you cache-control headers set such that intervening caches will flush out old copies of pages in a reasonable amount of time? Have you flushed your browser cache before each test?

I can't figure out any other reasons why you would get intermittent behaviour.

I sure hope someone else spots something here, because I'm sure out of ideas. :(

Jim

htmanning

11:28 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



I'm really stumped too. Can you think of any reason why I would not be capturing the query string? The first thing after the? in the query is always siteid=555 and I tried to make the recepie based on that, and it still doesn't work. So, I don't think I'm actually capturing the query string.

htmanning

11:35 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



Oh man...I goofed. Here's the deal. There is no query string on the link. The query string is on the page that contains the link to software.html. In other words, to get to the page that has the link to software.html there is a query string, but that page links directly to software.html. That's why I'm not capturing anything. Sheesh. Is there a way to route based on the query of the page preceeding? Does that make sense?

htmanning

11:46 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



Okay, I got it. Here's what works in case someone else needs help. And, thanks so much for the input. I was wrong here and gave you wrong info. This works:

RewriteCond %{HTTP_REFERER} .*platformname=Test.* [NC]
RewriteRule ^.*$ [abc.com...] [R=301,L]

That's the only thing that works and it's working well.

Thanks.

jdMorgan

11:56 pm on Feb 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to hear you got it working!

You can save a little mod_rewrite parser time by simply using:


RewriteCond %{HTTP_REFERER} platformname=Test [NC]
RewriteRule .* http://www.example.com/software.html? [R=301,L]

Which is entirely equivalent in this case.

Ref: [etext.lib.virginia.edu...]

Jim