-2

I don't know the correct term for what I am in need of so I apologize in advance for the misleading title.

When I code in PHP I usually use the ?idName=VALUE such as:

http://domain.com/phpFileName.php?idName=VALUE

What I would like to do instead of that is:

http://domain.com/mysites/ysub

Live example: http://www.probuilds.net/guide/EUW/2263803659/52210396

Stackoverflow uses the same style but I don't understand how it works?

Muki
  • 51
  • 8
  • 1
    That's two things actually: (1) "[Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/q/20563772)" and (2) mapping titles or "slugs" to database internal / numeric ids. (Your PHP needs to be adapted for that, and your tables need alternative identifiers for things.) – mario Aug 25 '15 at 01:23
  • Got familiar with http `POST` method? – itwasntme Aug 25 '15 at 01:33

2 Answers2

0

Search for pretty or semantic URLs.

First part is usually handled by a webserver, which converts your pretty URL to "classic" style URL pointing to some script and adding the rest of URL as an URL parameter. This is usually called URL rewrite.

For example:

http://example.com/this/is/pretty/url is rewritten to http://example.com/script.php?id=this/is/pretty/url

Then you just have a variable $_GET['id'] containing the string "this/is/pretty/url" (usually called slug) instead of some integer ID in your PHP script. Then it's up to you how you'll handle it in PHP.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
0

To see how it works, create a test.php script like this:

<?php
  print_r(explode('/', $_SERVER['REQUEST_URI']));
?>

Then call the script like this:

http://domain.com/test.php/abc/def

You will see this output:

Array ( [0] => [1] => test.php [2] => abc [3] => def )

This is possible because Apache reads back the URL, element by element, until it finds a valid file. When it does, it process the file and informs the full URI in REQUEST_URI server variable, so you can read and use it as parameters for your PHP script.

The rest is done like Dawid pointed out, using the rewrite Apache module to hide the php script (test.php, in this example), to make the URL friendly.

Marcovecchio
  • 1,322
  • 9
  • 19