phpforms.htaccessget

.htaccess rewrite url when using get values


I'm trying to turn http://www.placeholder.com/lala.php?fname=bob into http://placeholder.com/lala/bob

      <form action="lala.php" method="get">
    Name: <input type="text" name="fname">
    <input type="submit">
    </form> 
Welcome <?php echo $_GET["fname"]; ?>.<br>

I tried this on the .htaccess, but nothing happened

Redirect 302 /lala.php?fname=bob http://www.placeholder.com/lala/bob.php

Is this possible?


Solution

  • If you want to send visitors from http://www.starsqa.com/lala/bob to http://www.starsqa.com/lala.php?fname=bob:

     RewriteCond %{QUERY_STRING} ^$
     RewriteRule ^/lala/bob$ /lala.php?fname=bob [L,QSA]
     RewriteCond %{QUERY_STRING} ^$
     RewriteRule ^/lala/bob/$ /lala.php?fname=bob [L]
    

    If you want a general pattern where fname can be any name, try this:

     RewriteCond %{QUERY_STRING} ^$
     RewriteRule ^/lala/([a-zA-Z0-9:\@.\-\+]{1,100})$ /lala.php?fname=$1 [L,QSA]
     RewriteCond %{QUERY_STRING} ^$
     RewriteRule ^/lala/([a-zA-Z0-9:\@.\-\+]{1,100})/$ /lala.php?fname=$1 [L]
    

    If instead you want to send visitors from http://www.starsqa.com/lala.php?fname=bob to http://www.starsqa.com/lala/bob :

     RewriteCond %{QUERY_STRING} ^$
     RewriteRule ^/lala.php?fname=bob$ /lala/bob [L]
    

    And similarly, a more general pattern for this would be:

     RewriteCond %{QUERY_STRING} ^$
     RewriteRule ^/lala.php?fname=([a-zA-Z0-9:\@.\-\+]{1,100})$ /lala.php/$1 [L]
    

    Last Example (sending a user from starsQA.com/jen-lilley-contact to starsQA.com/contact-id=6) :

     RewriteCond %{QUERY_STRING} ^$
     RewriteRule ^/jen-lilley-contact$ /contact-id=6 [L]
    

    By the way, do not forget to restart Apache.

    How to restart Apache depends on your platform configuration. What OS are you running it on?