DenBeke
(DenBeke)
February 20, 2017, 9:30am
1
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}
}
abiosoft
(Abiola Ibrahim)
February 20, 2017, 5:38pm
3
# 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
system
(system)
Closed
May 22, 2017, 1:58pm
5
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.