Regex and if = almost always bad in nginx.
Almost all of your regex rewrites in nginx are going to seriously impact performance here.
For your initial routing for pretty URLs, you can actually use this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Laravel is smart enough to look in $_SERVER["PATH_INFO"]
. It also handles trailing slashes.
Routing
You can then route the search you plan to do like so:
Route::any("/results/{search?}", "Search@results"); // ? = optional
This notation is Class@method
. It's not static.
In app/controllers/Search.php
, you'd have the following:
<?php
class Search extends BaseController {
public function results($search = null) {
if (!$search) {
if (Input::has("q")) {
// This is where you'd do SEO cleanup to remove special chars.
return Redirect::to("/results/" . Input::get("q"));
}
} else {
// do stuff with the $search variable here
}
}
}
When you do rewrites in nginx, you actually redirect the user anyways. (via a 301, 302 or 308 redirect).
You can avoid this extra request with javascript (send the browser to /request/search-term
on submit), and you'd have a decent chunk of requests saved without impacting the experience of people who browse with noscript, too.