1

I am developing an educational app using PHP5, MySQL. I am using Slim as the framework to develop the APIs for the website as well as mobile apps.

I have various data models like Questions, Quizzes, Subjects, Topics etc. My current website has the following URL patterns:

  • www.site.com/subjects.php?subject_id=1
  • www.site.com/topics.php?topic_id=2
  • www.site.com/quizzes.php?id=3

I went through resources on URL rewriting using Apache, .htaccess. But this is still not very clear to me. I am looking for a solution that can enable me achieve user friendly and unpredictable url links. e.g.

  • www.site.com/subjects/science
  • www.site.com/topics/light-energy
  • www.site.com/quizzes/check-your-grammar-skills

However... these data depends on my database. What is the best way to achieve this without using a framework like laravel.

  • Sure, we can follow so far. What is unclear is what your specific question is. You do not show any of your code, your own attempt so far, so we cannot say where you have an issue. And you certainly did not just ask the question here to have us do your work for you, did you? – arkascha Aug 30 '17 at 21:10
  • 1
    You are using Slim, right? Did you read this part of their documentation https://www.slimframework.com/docs/objects/router.html#how-to-create-routes ? – Nima Aug 30 '17 at 21:13
  • Also it is not clear from your examples where the actual IDs should come from during the rewriting. Words like "science" or "light-energy" certainly are not the IDs. But the rewrite engine cannot _guess_ the IDs which apparently are not named inside the URLs... – arkascha Aug 30 '17 at 21:13
  • @VishalKumar "I am using", but you are not the OP, as far as we can tell... – arkascha Aug 30 '17 at 21:23
  • @VishalKumar If those are numeric IDs, then especially they need to be part of the requested URL. As said: the rewriting engine cannot somehow magically guess which specific ID you refer to with your URL. if that numeric ID is not part of the URL. I added an answer below explaining a possible and typical approach. – arkascha Aug 30 '17 at 21:25
  • Hm, .... @VishalKumar has removed his comment again. Looks like someone is using multiple accounts? – arkascha Aug 30 '17 at 21:37

1 Answers1

2

Either your question is unclear or you have not (yet) really thought this through... If you want to rewrite pretty URLs into an internal request that refers to some specific object inside your data model, then you do need that object's ID inside the URL. Simply because the http servers rewriting logic cannot somehow guess that (typically numeric) ID somehow.

So I assume you want to finally use URLs along this scheme:

For that situation you'd need a rewriting set like this:

RewriteEngine on
RewriteRule ^/?subjects/(\w+)/(\d+)$ /subjects.php?subject=$1&subjectid=$2 [END]
RewriteRule ^/?topics/(\w+)/(\d+)$ /topics.php?topic=$1&topic_id=$2 [END]
RewriteRule ^/?quizzes/(\w+)/(\d+)$ /quizzes.php?quizz=$1&id=$2 [END]

I personally would not use different names for the ID comlumn, but that is a question of personal choice and of how you internally match, I guess.

The above rule set will work in the http servers host configuration and likewise inside some dynamic configuration file (".htaccess" style file). you obviously need to have the http servers rewriting module loaded and activated. In case you decide to use a dynamic configuration rule also need to make sure that it's interpretation is enabled (see the AllowOverride directive in the documentation) and that the file is located in the http hosts DOCUMENT_ROOT and readable by the server process.

In case you get a http status 500 back for requests to those URLs ("internal server error"), then chances are that you operate a very old version of the apache http server. In that case try using the [L] flag instead of the [END] flag.


And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Nice Info. I have researched this topic ...though not in depth... Can you please explain what do you mean when you said - "if you have an application that relies on writing its own rewrite rules" ? – Digital Wolf Aug 30 '17 at 21:32
  • 1
    @DigitalWolf Quite a number of applications have a builtin logic that modifies their very configuration. That is done by the application changing some dynamic configuration file. This is flexible and allows an easy setup especially on free web space service offers which typically do not grant you access to your http servers host configuration (because you share the http server with thousands of other customers). The issue is that this potentially also allows attackers to change the configuration of your application... (examples would be wordpress, drupal, nextcloud, ...) – arkascha Aug 30 '17 at 21:35