'htaccess multiple parameter values not working properly

I want to get this URL:

www.example.com/scooter-details/1/vespa-sprint-matt-midnight-blue

But the URL I get is:

www.example.nl/scooter-details/1/Vespa/-Sprint%20-%20Matt%20Midnight%20Blue

Question

How can I delete all the white spaces %20 and replace it with hyphen characters (-). Also, is it possible to change the QUERY STRING Vespa/-Sprint%20-%20Matt%20Midnight%20Blue to lowercase vespa/-sprint-matt%20Midnight%20Blue

Here is the htaccess code

Options +FollowSymLinks -MultiViews
RewriteEngine On

# SEO FRIENDLY URL

# Redirect "/scooter-details.php?scooter_id=<num>&scooter_brand<brand>&scooter_model<model>" to "/scooter-details/<num>/<brand>-<model>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^scooter_id=(\d+)&scooter_brand=([^/.]+)&scooter_model=([^.]+)$
RewriteRule ^(scooter-details)\.php$ /$1/%1/%2/$2-%3$3 [QSD,L,R=301,NE]
    
# Rewrite /scooter-details/<num>/<brand>-<model>" back to "/scooter-details.php?scooter_id=<num>&scooter_brand<brand>&scooter_model<model>"
RewriteRule ^(scooter-details)/(\d+)/([^/]+)$ $1.php?scooter_id=$2&scooter_brand=$3&scooter_model=$4 [L]


Solution 1:[1]

You should implement that redirect in your PHP code rather than in .htaccess. That type of processing is hard to do in .htaccess, but easy to implement in PHP. You should remove your QSD rewrite rule and its conditions.

So that your PHP script can tell when it should redirect vs when it shouldn't, pass an additional parameter in your last rewrite rule:

# Rewrite /scooter-details/<num>/<brand>-<model>" back to "/scooter-details.php?scooter_id=<num>&scooter_brand<brand>&scooter_model<model>"
RewriteRule ^(scooter-details)/(\d+)/([^/]+)$ $1.php?redirect=no&scooter_id=$2&scooter_brand=$3&scooter_model=$4 [L]

If your PHP script sees the redirect=no parameter, it should produce the page. If that parameter is missing, it should build the canonical URL and redirect to it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephen Ostermiller