1

I'm working on a simple app to retrieve a JSON file info, but to do so I need an ID, I fixed that problem by adding it to the URL like this:

http://localhost:8000/comic_library/comic.php?id=2097

Since this is not a friendly URL, I change so it looks like this:

http://localhost:8000/comic_library/comic/2097

But when I retrieve the URL, the only thing I got back is:

http://localhost:8000/comic_library/comic.php

But I need the id in order to retrieve the JSON file, is there a way of getting the custom URL and not just the absolute URL

  • Possible duplicate of [URL rewriting with PHP](https://stackoverflow.com/questions/16388959/url-rewriting-with-php) – iamabhishek Jan 16 '19 at 05:59

3 Answers3

1

As you are using core php, it is not possible for you to have an url like http://localhost:8000/comic_library/comic/2097, if you used a framework like codeigniter or cms like wordpress then you would get that url. Core php urls have .php extension.

Shoyeb Sheikh
  • 2,659
  • 2
  • 10
  • 19
0

You should Pass the URL like this http://localhost:8000/comic_library/comic.php/2097

$uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $uri_segments = explode('/', $uri_path);
    echo $uri_segments[2]; // for http://localhost:8000/comic_library/comic.php/2097 you will get '2097'

First split the URL segments and Get the desired segment array value.

Taki Elias
  • 127
  • 1
  • 7
0

if your url is this

http://localhost:8000/comic_library/comic/2097

you can get your id by the following process

  $fullurl = 'http://localhost:8000/comic_library/comic/2097';
  $exploded_url = explode('/',$fullurl);
  echo $final_id = end($exploded_url);
Abid
  • 344
  • 1
  • 4
  • 21