1

I am trying to get the value after the / in a URL in PHP.

I have tried using $_GET['va'], but this only works for the following,

http://localhost:8080/default?va=xxx

What I'm trying to get is this,

http://localhost:8080/default/xxx

How do I get the xxx value after a / in PHP.

Any help is greatly appreciated.


Edit

Thanks to everyone who answered, I wasn't very clear in stating what I wanted. It appears what I was looking for is known as a pretty URL or a clean URL.

I solved this by following Pedro Amaral Couto's answer.


Here is what I did;

I modified my .htaccess file on my Apache server, and added the following code.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ default.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ default.php?page=$1

Then I modified my default.php file to GET ['page']

<?php
   echo $_GET['page'];
?>

And it returned the value after the "/".

ddr45
  • 123
  • 9

5 Answers5

2

You are looking for: $_SERVER['REQUEST_URI']

The URI which was given in order to access this page; for instance, '/index.html'.

dani herrera
  • 44,444
  • 7
  • 103
  • 165
  • You can't use that if you can't use the URL without a 404 error. – Pedro Amaral Couto Apr 04 '18 at 15:02
  • 1
    Hi @PedroAmaralCouto, obrigado pelo seu comentário! May be OP is asking for `/default/xxxx/my_php_page.php` ;) You are guessing he/she is asking for pretty urls but is is not clear. I just answering the question: *"how to get value after / in URL"* – dani herrera Apr 04 '18 at 15:10
2

You want to make what is called "pretty URLs" (and other names).

You need to configure the HTTP server appropriately, otherwise you'll get a 404 error. If you're using Apache, that means you may configure .htaccess with RewriteEngine module activated. You also need to add regular expressions.

There's already a question in StackOverflow concerning that subject:

Here are another relevant articles that may help:

You can see how it's done in Wordpress:

If you follow those, you won't need to change the PHP code, you may use $_GET to retrieve "xxx".

Pedro Amaral Couto
  • 1,418
  • 11
  • 11
1
basename(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
James Lingham
  • 409
  • 1
  • 3
  • 16
1

So the $_GET global variable is for parsing the query string.

What you're looking for is the $_SERVER['REQUEST_URI'] global variable:

$url = $_SERVER['REQUEST_URI'];

$url will now contain the full URL of your path. You'll need to use explode('/', $url) to break up that full URL into an array of little strings and parse it from there.

Example:

$pieces = explode('/', $url);
// this will get you the first string value after / in your URL
echo $pieces[0];
Dylan Pierce
  • 3,711
  • 3
  • 30
  • 41
1

You can do in 2 ways.

1- do these steps

  • Get URL
  • Explode by /
  • Get Last element of array

2- Add .htaccess and map that value for some variable

RewriteRule ^default/(.*) page.php?variable=$1

and you can get $_GET['variable']

Naveed Ramzan
  • 3,509
  • 3
  • 24
  • 28