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

5
  • 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. Commented Sep 9, 2016 at 8:48
  • Can I kindly ask you to provide real life working example? Commented Sep 9, 2016 at 8:50
  • AIUI you're pretty much asking for the reverse of this: stackoverflow.com/questions/18406156/… < 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)? Commented Sep 9, 2016 at 8: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 Commented Sep 9, 2016 at 8: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. Commented Sep 9, 2016 at 8:55

1 Answer 1

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> 
Sign up to request clarification or add additional context in comments.

3 Comments

Hey @ChrisLear this looks like really nice workaround for $_GET but it looks $_POST doesn't work at all :(
$_POST shouldn't be affected - this is merely routing $_GET requests...
I take it back, it works correctly just had to use file_get_contents('php://input');

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.