RewriteRule Pattern Substitution [flags] tests a PCRE pattern against the URL path (in .htaccess, the part below the directory; RewriteBase). The substitution is a path relative to the directory, a URL path starting with /, an absolute URL (an external redirect), or -, which changes nothing so that a flag such as [F] (403) or [G] (410) can act alone. Pattern groups become $1 to $9 ($0 is the whole match), groups in the last matched RewriteCond become %1 to %9, and (?:...) groups without capturing:
RewriteEngine On
RewriteRule ^products/(\d+)(?:/([a-z0-9-]+))?/?$ show.php?id=$1&slug=$2&all=$0 [L]
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.example\.com$
RewriteRule ^shop/(.*)$ show.php?store=%1&path=$1 [L]$ t /products/42/blue-mug /products/42 /products/42/Blue-Mug
/products/42/blue-mug 200 {"id":"42","slug":"blue-mug","all":"products\/42\/blue-mug"}
/products/42 200 {"id":"42","slug":"","all":"products\/42"}
/products/42/Blue-Mug 404
$ \curl -s -H 'Host: uk.example.com' localhost:8105/shop/mugs/tall | grep get
get: {"store":"uk","path":"mugs\/tall"}An unused optional group yields an empty string, Blue-Mug failed the lowercase class, and $0 lacks a leading slash (RewriteBase). \curl 3,008 bypasses the alias so the request carries its own host, from which %1 took uk. Captures are inserted decoded, so a captured x & y would split the query string at the &; the [B] flag re-escapes them (Rewrite Flags).