Been reading about this for a while now and I’m close but struggling with the final hurdle.
I have a url like this:
site.com/detail/123/some-description/maybe-some-more-description
that I want to become
site.com/details.php?id=123
I’ve got this so far
RewriteRule detail/(.*)/.*$ details.php?id=$1
which according to this (https://htaccess.madewithlove.be/) results in
details.php?id=123/some-text
how do I get rid of the some-text on the end?
3
Answers
It seems that in your case you have one more group of
/text/
Change your rule from:
RewriteRule detail/(.*)/.*$ details.php?id=$1
to
RewriteRule detail/(.*)/(.*)/.*$ details.php?id=$1
If you don’t know how many slashes there are you can just use group with excluded slash
[^/]*
:RewriteRule detail/([^/]*)/.*$ details.php?id=$1
Try this:
(.*?detail)/(d+)/.*
$1.php?id=$2
Which results in
site.com/detail.php?id=123
.They both can be tested at Regex101.
(.*?detail)
matches the site base URL withdetails
and captures to the group$1
./
matches the slash itself (must be escaped).(d+)
matches the number and captures it to the group$2
./.*
matches the rest of the URL and it helps to not include it in the replacement.Edit: Do you mind the difference between
detail
anddetails
?RewriteRule ^([A-Za-z0-9]+)$ file.php?key=$1 [L,QSA]
use in anchor tag
if you want receive value in variable $value = $_REQUEST[‘key’];