skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. Try this:

    • The regex: (.*?detail)/(d+)/.*
    • The replacement: $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 with details 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 and details?

    Login or Signup to reply.
  3. RewriteRule ^([A-Za-z0-9]+)$ file.php?key=$1 [L,QSA]

    use in anchor tag

      <a href="details/<?php $id?>">
    

    if you want receive value in variable $value = $_REQUEST[‘key’];

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search