Rewrite rules with parameters

How can I extract parameters from an URL with regex?
I need to convert this .htaccess file?

#One url parameter
RewriteRule ^([a-zA-Z0-9_\-]*)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_\-]*)$ index.php?page=$1

#Two url parameters
RewriteRule ^([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)$ index.php?page=$1&album=$2
RewriteRule ^([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)/$ index.php?page=$1&album=$2

#Three url parameters
RewriteRule ^([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)$ index.php?page=$1&album=$2
RewriteRule ^([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)/$ index.php?page=$1&album=$2

Hi @DenBeke,

There’s a few examples at the bottom of the Caddy rewrite docs that effectively illustrate using regex and parameters in rewrites.

Generally it takes the form:

rewrite {
    r  (.*)
    to /index?path={1}
}
# One url parameter
rewrite {
    r  ^([a-zA-Z0-9_\-]*)/$
    to /index?path={1}
}

# Two url parameters
rewrite {
  r ^([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)$
  to /index.php?page={1}&album={2}
}

# Three url parameters
rewrite {
  r ^([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)/([a-zA-Z0-9_\-]*)$
  to /index.php?page={1}&album={2}&artist={3}
}
3 Likes

Thanks, @abiosoft!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.