Wednesday, April 6, 2011

Mod Rewrite - Redirecting based on having GET parameters or not

I want to redirect these URLs:

http://example.com/search to http://example.com/?page=search

http://example.com/search?q=keyword to http://example.com/?page=search&q=keyword

I am trying to use this in .htaccess but doesn't work:

RewriteRule ^search$ ?page=search [nc]
RewriteRule ^search\?(.*)$ ?page=search&$1 [nc]

What am I doing wrong?

From stackoverflow
  • Assuming you want to direct the request to the index.php file include the file in the rules. E.g.

    RewriteRule ^search$ index.php?page=search [L]
    

    Give it a try.

    XaviEsteve : Hi Andreas, it is working for the "/search" but not when adding parameters. Any ideas for passing the parameters?
    andreas : RewriteRule ^search/([^/])+?$ index.php?page=search&q=$1 [L] (please try and let me know)
    andreas : Also check out: http://www.modrewrite.com/
    XaviEsteve : Thanks Andreas! I've replaced the `+` to `*` and moved the quantifiers inside the parenthesis and it works now. Here it is: `RewriteRule ^search/([^/]*?)$ index.php?page=search&q=$1 [L]`.

0 comments:

Post a Comment