Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Htaccess - RewriteRule - Url with Arguments

    Hi,

    I want to replace:

    http://www.site.com/index.shtml?red

    with:

    http://www.site.com/?widget=red

    I tried (engine is on etc):

    RewriteRule ^index\.shtml?red$ http://www.site.com/?widget=red [r=301,l]

    and

    RewriteRule ^index\.shtml\?red$ http://www.site.com/?widget=red [r=301,l]

    Without effect, however this:

    RewriteRule ^index\.shtml$ http://www.site.com/?widget=red [r=301,l]

    Works.

    What do I do wrong?
    Thanks,
    Jerome

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    You could do a few things depending on your needs.

    1) if you want any query string added, you can use [QSA] (Query String Append)
    Code:
    RewriteEngine On
    RewriteRule ^index\.shtml$ http://someserver.tld/something.php [R=301,QSA,L]
    2) if you want only to redirect if the value is red, you would do this
    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(red)$
    RewriteRule ^index\.shtml$ http://someserver.tld/something.php?%1 [R=301,L]
    3) if you want only to redirect one of a set of values (red or blue, split by a pipe) you could do this
    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(red|blue)$
    RewriteRule ^index\.shtml$ http://someserver.tld/something.php?%1 [R=301,L]
    Edit, if it's the same domain you don't need it, just /something.php
    Edit2: I said [QSA] was optional on the last 2, it's not (although it could be).
    Last edited by schleppel; 08-22-2005 at 06:43 PM.

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Posts
    299
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi schleppel,

    Option 2 is the best solution I think, but there is something going wrong
    In my case it should look like:

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(red)$
    RewriteRule ^index\.shtml$ http://someserver.tld/?widget=red%1 [R=301,L]

    In the title I see quickly:
    http://someserver.tld/?widget=red&red

    Which is already not ok (&red to much)

    But what I feel very strange is that the url in the address-bar doesn't change, is this normal? It's a redirect not?

    Thanks jerome

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    On my test server this works fine.
    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^(red)$
    RewriteRule ^index\.shtml$ http://82.36.227.98/?widget=%1 [R=301,L]
    Your &red problem was that %1 holds the value of red, you don't need it if your hardcoding it (it's used for multiple ones), but it's better if you don't, as then you only need to change one value.

    I have no idea why it doesn't redirect.


  •  

    Posting Permissions

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