0

i followed up multiple tutorials how to make pretty URL but never actualy make it work (prolly i didnt get something).

What i want:

From something like this:

http://www.example.com/api/v1/get.php?user=UserName&id=7Ka2la2

I want to make something like this:

http://www.example.com/UserName/get/7Ka2la2

What i did try: As I mentioned I try to follow multiple tutorials but nothing worked for me. So i try something by my self.

//.htaccess
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]
</IfModule>

What it does:

  • it checks if the request filename isn't a file
  • and checks if it isn't a directory
  • then, the RewriteRule makes a call to index.php, no matter what was written in the URL

And in my index.php file it looks like this

<?php
function parse_path() {
  $path = array();
  if (isset($_SERVER['REQUEST_URI'])) {
    $path = explode('/', $_SERVER['REQUEST_URI']);
  }
return $path;
}

$path_info = parse_path();
echo '<pre>'.print_r($path_info, true).'</pre>';

switch($path_info[1]) {
  case 'get': include 'get.php';
    break;
  default:
    include '404.php';
}

So it basicly should just split url to array and then base on URL include right file (in this example its get.php). However like this i can load a file but i have nothing in my $_GET and $_POST which make my script useless for me.

Question: My code will somehow do what i want so base on url it load content but $_GET and $_POST will not work correctly here. So my question is did I make it wrong way? If yea how should looks the right one and if not how I can access $_GET and $_POST variabiles

Andurit
  • 5,612
  • 14
  • 69
  • 121
  • You'd normally use `mod_rewrite` the other way around - when you want to make your *"pretty"* URL actually functional : `RewriteRule ^ index.php [L]` is basically stripping all your GETdata. – CD001 Sep 09 '16 at 08:48
  • Can I kindly ask you to provide real life working example? – Andurit Sep 09 '16 at 08:50
  • AIUI you're pretty much asking for the reverse of this: http://stackoverflow.com/questions/18406156/redirect-all-to-index-php-htaccess/18406686#18406686 < that will take a *"pretty"* URL and make it functional; you're asking for something to make a functional URL pretty (which would then need to be manipulated to become functional again)? – CD001 Sep 09 '16 at 08:51
  • If i understand this link correctly it transform ugly url to nice one. but does it mean user will have to use ugly url to get on nice one or he can directly use that nice one and it will works – Andurit Sep 09 '16 at 08:53
  • No it transforms the nice URL to the functional one ... so if the URL is `example.com/this/is/restful` (which appears in the address bar) that request gets redirected (invisibly) to `example.com/index.php?path=this/is/restful` which the application then has to process. The problem, of course, is that you have to generate the RESTful URLs in the first place. – CD001 Sep 09 '16 at 08:55

1 Answers1

2

You can set$_GET yourself. $_POST is unaffected by the rewrite.

Try this if you like:

<?php
function parse_path() {
  $path = array();
  if (isset($_SERVER['REQUEST_URI'])) {
    $path = explode('/', $_SERVER['REQUEST_URI']);
  }
return $path;
}

$path_info = parse_path();
echo '<pre>'.print_r($path_info, true).'</pre>';

// SET UP $_GET HERE

$_GET['user'] = $path_info[0];
$_GET['id'] = $path_info[2];

switch($path_info[1]) {
  case 'get': include 'get.php';
    break;
  default:
    include '404.php';
}

But if you can still modify the code that looks for $_GET you might want to consider not using $_GET like this, and rather set up some sort of class that contains the values.

And you also might want to consider doing your url rewriting so as to map the original request to something that has get variables.

eg

//.htaccess
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.+)/(.+)/(.+)$ /api/v1/$2.php?user=$1&id=$3 [L]
</IfModule>
Chris Lear
  • 6,592
  • 1
  • 18
  • 26