1

Right now, upon form submission, the URL looks like:

(where 'query' is the submitted data)

http://mySite.com/index.php?search=query

or

http://mySite.com/?search=query

How can I make it

http://mySite.com/search?q=query

If possible to do it without editing the .htaccess file, then please tell me how.

Or if it is necessary to edit .htaccess, please teach me what rule to add so that all queries have a URL in that form when submitted.

11
  • why you need to use search?? remove that and use url as http://mySite.com/?q=query.. Commented Apr 17, 2013 at 5:34
  • you have to rewrite rules in .htaccess file Commented Apr 17, 2013 at 5:36
  • @saveATcode I want to make it clear that, in this case, the submission was a search. Commented Apr 17, 2013 at 5:58
  • @messifan what is the rule I need to add to get that Commented Apr 17, 2013 at 5:59
  • @IrfanMir: search?? what i see is search is the name of parameter in which you are passing "query" as data..am i wrong?? Commented Apr 17, 2013 at 6:02

2 Answers 2

1

I would recommend redirecting from index.php rather than use of rewrite rules in this case. Rewrite rules also have cost, and they will be checked on every page load

In the beginning of index.php:

<?php
if(!empty($_GET["search"]))
{
    header("Location: search?q=".rawurlencode($_GET["search"]));
    exit;
}
?>

As Olaf Dietsche rightfully noted in the comments, mod_rewrite outperforms PHP, so the question is really what is the hit ratio of index.php vs other files. If it is low, php approach is definitely better, however, if index.php hits 50% of the time or so, then you might be better off with rewriting.

Hence, adding mod_rewrite solution as well:

RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)search=([^&]+)
RewriteRule ^index.php$ /search?q=%1 [R,L]

RewriteCond actually looks for search= query string parameter and ensures it's not empty And the rule simply rewrites anything from index.php into /search?q=<query> Note that %1 used to match the first captured sub-pattern matched from the conditions (rather than from the matching part of the rule itself - in which case we would need to use $1).


However, the real question is why we are dealing with this problem in first place. If search is done by submitting a value in a page, then it's form submission and as such it can be pointed directly to the handler and not redirected there afterwards. This is much more efficient.

<form action="/search" method="get">
<input type="text" name="q">
...

This way, anything submitted from the search form will go directly to /search and will not need a redirect to deal with.

Sign up to request clarification or add additional context in comments.

11 Comments

Why use !empty(). Should you use is set()? That way when the form is submitted?
When everything is tunneled through index.php, as is almost always the case with PHP frameworks, using PHP for redirecting is even more expensive than mod_rewrite.
@IrfanMir if the form is submitted without a value, then this parameter is still passed, but empty. why search for empty value?
@poncha A simple performance test with ab on localhost shows otherwise. 10000 requests with PHP header: ~8.7 sec, 10000 requests with mod_rewrite: ~5.8 sec. That's +50% for using PHP. So, it all depends on the ratio of index.php vs other files.
@IrfanMir This looks like a coin toss. But with a ratio 30/70, you should be fine with poncha's solution. Depending on the amount of traffic you have, this might be a non-issue anyway. So unless you have lots of requests, you should choose the solution, you're most comfortable with.
|
1

To add a mod_rewrite solution. You must check for index.php and a search= query string

RewriteCond %{QUERY_STRING} search=(.*)
RewriteRule ^index\.php$ /search?q=%1 [R,L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.