0

I have an App using URLs such as

/order?123 and I'm using $_SERVER['QUERY_STRING'] to get the variable but I want to change the URLs to a more readable form like

/order/123

How can I get 123?

4 Answers 4

2

Apache needs to know you want to run your script when such a request is seen. Assuming your script is named order.php put this in a .htaccess file in the same directory as your script

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/order/(\d+)*$ ./order.php?id=$1

eg; /order/123 is treated as /order.php?id=123
you get that value with ..

echo $_REQUEST['id'] 
Sign up to request clarification or add additional context in comments.

Comments

0
$str = '/order/123';
echo substr($str, strrpos($str, '/') + 1);

Try this.

Comments

0

You can parse $_SERVER['REQUEST_URI']:

<?php
    $uri = $_SERVER['REQUEST_URI'];
    $var = substr(strrchr($uri, '/'), 1);
?>

As always, don't forget to check, sanitize, validate, escape this variable.

Comments

0

I'm assuming you have a mistake in your /order?123 URL, I'm assuming it should be using get variables so /order?var=123. With that assumption the way to get variables from the URL is with parse_str... like this...

$arrVariables = array();
parse_str($str, $arrVariables);
$arrVariables['var'];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.