简体   繁体   中英

Rewrite url from parameter into subdir

As search by parameters, I want url display subdir instead parameter like this:

Here params url: search/?genre=a&pref=b&city=c&sticking&=d

And here is subdir url after convert for display: search/a/b/c/d

In the case search url has less than 4 params, it can be converted to some urls like this:

search/a/b/c
search/b/c/d
search/a
search/b
...

Is there any way to use htaccess to do that?

I'm using Laravel.

Thank you for your answer.

In your /route/web.php or /route/api.php file, you can define the route like below:

Route::get('/search/{a?}/{b?}/{c?}/{d?}', 'SearchController@method');

In the controller as suggested by Rian Zaman, you have to do something like this:

public function method_name($a = null, $b = null, $c = null, $d = null)
{
    // Your code here ...
}

If you want to achieve that I would suggest to use optional route parameters if you know what could be the max number of search term.

You can achieve that by doing the following,

In your route:

Route::get('/your-route/{param1?}/{param2?}/{param3?}/{param4?}');

In your controller:

public function function_name($param1 = null, $param2 = null, $param3 = null, $param4 = null){
// your logic
}

Please follow the link for reference: Optional parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM