Forum Moderators: phranque
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.
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]
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]
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]
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
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.
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
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.
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
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.
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]
Ref: [etext.lib.virginia.edu...]
Jim