Rewriting urls with mod_rewrite (htaccess)

Discussion in 'Apache' started by tmekt, Jul 10, 2010.

  1. #1
    So, I have a site which currently has urls like this:

    /?s=page&foo1=bar&foo2=bar&foo3=bar

    and I would want them to display like this:

    /page?foo1=bar&foo2=bar&foo3=bar


    S defines the current page and other variables define other things on different pages.

    Can you give me some advice how to do this in .htaccess-file? I'm not very experienced with Apache's mod_rewrite so if you can go easy on me, all help will be appreciated. Thanks to all already in advance.
     
    Last edited: Jul 10, 2010
    tmekt, Jul 10, 2010 IP
  2. Imran

    Imran Notable Member

    Messages:
    2,340
    Likes Received:
    190
    Best Answers:
    0
    Trophy Points:
    230
    #2
    RewriteCond %{QUERY_STRING} ^s=page&foo1=bar&foo2=bar&foo3=bar$
    ReWriteRule $ /page?foo1=bar&foo2=bar&foo3=bar


    Please check if it works...
     
    Imran, Jul 10, 2010 IP
  3. tmekt

    tmekt Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the assistance, have one more question, though.

    Page-part and those bar-variables change as user requests from a php script so it has to be dynamically changeable also in htaccess. How do I do this?
     
    tmekt, Jul 10, 2010 IP
  4. Imran

    Imran Notable Member

    Messages:
    2,340
    Likes Received:
    190
    Best Answers:
    0
    Trophy Points:
    230
    #4
    Try this code and see if it works.
    RewriteCond %{QUERY_STRING} ^s=page&foo1=bar&foo2=bar&foo3=bar$
    ReWriteRule $ /page?foo1=bar&foo2=bar&foo3=bar

    Or Try this one....

    RewriteCond %{QUERY_STRING} ^s=page&([^/\.]+)([0-9]+)=bar&([^/\.]+)([0-9]+)=bar&([^/\.]+)([0-9]+)=bar$
    ReWriteRule $ /page?$1=bar&$2=bar&$3=bar

    Even I'm learning so lets see if it works.
     
    Last edited: Jul 10, 2010
    Imran, Jul 10, 2010 IP