0

I'm working on a project in PHP and I want my URLs to look friendly user, so this is what I currently have:

www.site.com/index.php?view=1
www.site.com/index.php?view=2&id=21
www.site.com/index.php?view=3&id=88

And this is what I expect to have:

www.site.com/teacher
www.site.com/student?id=21
www.site.com/book?id=88

Do you think this will work in the htaccess?

RewriteRule ^teacher$    index.php?view=1
RewriteRule ^student/(\d+)$    index.php?view=2&id=$1
RewriteRule ^book/(\d+)$    index.php?view=3&id=$1
JohnP
  • 33
  • 6
  • 1
    The general concept you are looking for is called “routing”, and the controller code is called a router. If you search for “PHP simple router” or similar you’ll find a bunch of examples, ranging from simple RegEx, to arrays, to full blown frameworks. – Chris Haas Oct 16 '21 at 13:55
  • Somewhere, you have to configure Apache to pass the requests to a PHP script rather than looking for a file called "teacher". That can be a set of specific rules for each URL, or it can be a single rule to pass everything to a single PHP script, and then code the specific rules in PHP. Either way, the principle is the same: you tell Apache what URLs to expect from the browser, and what you want it to do with them. – IMSoP Oct 16 '21 at 14:05

0 Answers0