htmanning has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, This isn't exactly a Perl question but I wonder if someone knows how to handle .htaccess redirects with a query string. I'm trying to forward some phpbb forums. This doesn't work:
RewriteEngine on RewriteRule /forums/viewtopic.php?f=12 http://www.newdomain.com/forums +/
It seems the query string hangs it. Any ideas?

Replies are listed 'Best First'.
Re: Redirects with query string
by moritz (Cardinal) on Dec 02, 2008 at 08:34 UTC
    The query string is separated from the URL before matching, as the docs clearly say:

    What is matched?

    The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.

      I got this to work (in case anyone needs the same thing).
      RewriteEngine on RewriteCond %{QUERY_STRING} ^f=12 RewriteRule ^viewforum.php http://www.newdomain.com/forums [R=301]
      That makes anyone coming to

      http://www.mydomain.com/forums/viewforum.php?f=12

      get redirected to

      http://www.newdomain.com/forums.

        Nice work thanks. The only difference I but (.) in the end. RewriteRule ^viewforum.php http://www.newdomain.com/forums. [R=301] thanks
Re: Redirects with query string
by Anonymous Monk on Dec 02, 2008 at 08:32 UTC
Re: Redirects with query string
by Anonymous Monk on Dec 02, 2008 at 20:02 UTC
    To do a true redirect, you need to pass the [R ] flag