1

Is it possible to rewrite an url with a question a mark in it?

For instance,

http://localhost/mysite/search?tag=mars

to become this,

index.php?url=search&tag[key_name]=mars

I tried with this, but no good at all,

RewriteRule ^search?tag=([a-zA-Z0-9\-]+)/?$  index.php?url=search&tag[key_name]=$1&type=tag [L,QSA]

Any suggestions?

EDIT:

For %1,

RewriteCond %{QUERY_STRING} (?:^|&)tag=([^&]+)
RewriteRule ^search$ index.php?url=search&tag\%5Bkey_name\%5D=%1&type=tag [L]

Result,

print_r($_GET);

Array
(
    [url] => search
    [tag] => Array
        (
            [key_name] => 
        )

    [type] => tag
)

For %0,

RewriteCond %{QUERY_STRING} (?:^|&)tag=([^&]+)
RewriteRule ^search$ index.php?url=search&tag\%5Bkey_name\%5D=%0&type=tag [L]

Result,

print_r($_GET);

Array
(
    [url] => search
    [tag] => Array
        (
            [key_name] => tag=mars
        )

    [type] => tag
)
Salman A
  • 262,204
  • 82
  • 430
  • 521
Run
  • 54,938
  • 169
  • 450
  • 748
  • The `%{QUERY_STRING}` is handled separately from the path. Use a `RewriteCond`. – mario Feb 23 '13 at 18:20
  • possible duplicate of [Replacing a querystring parameter value using mod\_rewrite](http://stackoverflow.com/questions/1497184/replacing-a-querystring-parameter-value-using-mod-rewrite) – mario Feb 23 '13 at 18:21

1 Answers1

2

Query strings cannot be matched inside RewriteRule. You must use RwwriteCond instead:

RewriteCond %{QUERY_STRING} (?:^|&)tag=([^&]+)
RewriteRule ^search$ index.php?url=search&tag\%5Bkey_name\%5D=%1&type=tag [L]

Here:

  • [ and ] are urlencoded as %5b and %5D
  • \% is used to escape the literal % characters
  • %0 ... %9 are the strings that were matched/captured in RewriteCond clause. This is just the way you use $0 ... $9 for RewriteRule
  • QSA flag is not required
  • (^|&) matches beginning of input (e.g. in tag=mars&foo=bar) or & (e.g. in foo=bar&tag=mars)
  • ?: makes mod_rewrite not capture this group (other this match would go in %1, next one in %2, etc)

Your PHP script will get:

$_GET["url"]  : search
$_GET["tag"]  : Array([key_name] => mars)
$_GET["type"] : tag
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • thanks for the answer. I just tried but the `mars` is not passed into `key_name`. what does `%1` mean? – Run Feb 23 '13 at 18:27
  • thanks for the update. I still get nothing in `key_name`... I don't get it! – Run Feb 23 '13 at 18:32
  • Can you do a `var_dump($_GET)` and post results? – Salman A Feb 23 '13 at 18:35
  • Thanks. I have put the results in my edit above. with `%0`, I get an interesting result. – Run Feb 23 '13 at 18:43
  • I think it is to do with `(?:^|&)tag=([^&]+)`. Can I ask - what `(?:^|&)` and `([^&]+)` mean? – Run Feb 23 '13 at 18:50
  • I looked at your output, it is not possible for `$0` to contain `tag=mars` while `$1` is `""`. Something is fishy. Try removing `?:` (%1 becomes %2 in that case) and double check round brackets. – Salman A Feb 23 '13 at 18:57
  • yes it is now working with `(?:^|&)tag=([^&]+)` - `?:` is the key! thank you so much!! :-) – Run Feb 23 '13 at 18:58