Find the answer to your Linux question:
Results 1 to 5 of 5
Hi people, am a bit confused with this querystring appending stuff with mod_rewrite. I thought iv'e understood but my configuration PHP Code: < VirtualHost X . X . X . X ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2007
    Posts
    71

    Unhappy How to redirect requrest on apache2 virtualhost to append querystring


    Hi people,
    am a bit confused with this querystring appending stuff with mod_rewrite. I thought iv'e understood but my configuration


    PHP Code:

    <VirtualHost X.X.X.X:80>   
       
    ServerName  mypoductionserver.com   
       ServerAlias cs
    .myproductionserver.com   
       LogLevel warn   
       ErrorLog  
    /var/log/apache2/productionerror.log
      CustomLog 
    /var/log/apache2/productionaccess.log Combined 
      RewriteCond 
    %{QUERY_STRING} .   
     
    RewriteRule (.*) http://www.newproductionserver.com/? [R=301,L]  
    </VirtualHost

    doesn't seem to work. when i access www.myproductionserver.com/download?order_receipt=iyiuyiu it doesn't redirect to www.newproductionserver/download?orger_receipt=iyiuyiu.

    Same for www.myproductionserver.com/random?shortname=new
    it doesn't go to www.newproductionserver.com/random?shortname=new


    How can i solve that

  2. #2
    Linux Newbie
    Join Date
    Jan 2013
    Posts
    116
    Hi Highjo,

    Can you please try as below it should work:

    <VirtualHost X.X.X.X:80>
    ServerName mypoductionserver.com
    ServerAlias cs.myproductionserver.com
    Redirect permanent / http://www.newproductionserver.com/? [R=301,L]
    </VirtualHost>

  3. #3
    Linux Newbie nplusplus's Avatar
    Join Date
    Apr 2010
    Location
    Charlotte, NC, USA
    Posts
    106
    Looking at the apache docs for rewriting and the link you provided from the wiki, I think your rewritecond and rewriterule lines should look as follows given what you are trying to accomplish.


    Code:
    RewriteCond %{QUERY_STRING} ^(.*)$
    # The regex in the parens match anything    
     RewriteRule (.*) http://www.newproductionserver.com/$1?%1 [R=301,L]
    # The "$1" references/copies the rewriterule regex match (any path as you have), the "?" indicates what follows is a query string, and the "%1" references/copies the rewritecond query_string regex match.
    I think that is it. $1 might actually need to be $0, but I think it should be $1. %1 should definitely be %1 which will be a copy of everything in the parentheses of your query_string match.

    N

    [edit]

    Okay, I didn't read closely enough. Actually, you shouldn't need a rewritecond at all unless you want to rewrite only URLs with query strings. According to the apache docs, the query string should pass through untouched. On the other hand, if you only want to rewrite URLs with query strings, you should use the rewritecond, but your regex should look like .+ to match any query string of one or more of any characters. In either case, do not end your rewrite target with a question mark, as that will erase your query string. What I suggested before should also work, and might be more deterministic, but is unnecessary.

    [/edit]
    Last edited by nplusplus; 04-24-2013 at 03:19 AM. Reason: I need more sleep, and it's already late.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jan 2007
    Posts
    71
    Hi NixSavy Thanks for your answer, with your suggestion, apache restart failed, maybe there was a typo. So after that i've been looking online for better understanding of these things.

  6. #5
    Just Joined!
    Join Date
    Jan 2007
    Posts
    71
    Quote Originally Posted by nplusplus View Post
    Looking at the apache docs for rewriting and the link you provided from the wiki, I think your rewritecond and rewriterule lines should look as follows given what you are trying to accomplish.


    Code:
    RewriteCond %{QUERY_STRING} ^(.*)$
    # The regex in the parens match anything    
     RewriteRule (.*) http://www.newproductionserver.com/$1?%1 [R=301,L]
    # The "$1" references/copies the rewriterule regex match (any path as you have), the "?" indicates what follows is a query string, and the "%1" references/copies the rewritecond query_string regex match.
    I think that is it. $1 might actually need to be $0, but I think it should be $1. %1 should definitely be %1 which will be a copy of everything in the parentheses of your query_string match.

    N

    [edit]

    Okay, I didn't read closely enough. Actually, you shouldn't need a rewritecond at all unless you want to rewrite only URLs with query strings. According to the apache docs, the query string should pass through untouched. On the other hand, if you only want to rewrite URLs with query strings, you should use the rewritecond, but your regex should look like .+ to match any query string of one or more of any characters. In either case, do not end your rewrite target with a question mark, as that will erase your query string. What I suggested before should also work, and might be more deterministic, but is unnecessary.

    [/edit]

    Thank you the answer i found was pretty much close to your suggestion. i had to remove the Condition and the question mark and it worked. however i run into another issue which was that, it wasn't able to redirect any request from any mobile device(windows phone, blackberry , symbian, android).

    we use wurfl library with spring mvc to redirect to a wap friendly url an server a simple html page. It was too much to solve at a time so we just pointed the url to the ip of the new server old server .Since wasn't able to redirect properly, it can be say it was down. so we decided to let the domain propagate rather than touching an app which was working fine before:
    here is what worked before we change our mind:
    PHP Code:
    <VirtualHost X.X.X.X:80>
      
    ServerName mypoductionserver.com
      ServerAlias cs
    .myproductionserver.com
      LogLevel warn
      ErrorLog  
    /var/log/apache2/productionerror.log

      CustomLog 
    /var/log/apache2/productionaccess.log combined

      RewriteRule 
    (.*) http://www.newproductionserver.com/$1 [R=301,L] 
    </VirtualHost

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •