tags:

views:

56

answers:

6

so my index.php can be this:

<?php
      $restOfURL = ''; //idk how to get this
      print $restOfURL; //this should print 'FOO', 'VAR3', or any string after the domain.
 ?>
+1  A: 
$var = ltrim( $_SERVER['REQUEST_URI'], '/' )

http://www.php.net/manual/en/reserved.variables.server.php

Galen
hmm the only file that exists in the server is index.php. No folders or any other files exist..
jdc
A: 
$_SERVER['REQUEST_URI']
Mark
hmm the only file that exists in the server is index.php. No folders or any other files exist..
jdc
+3  A: 

You want to use,

<?php
    $restOfURL = $_SERVER['REQUEST_URI'];

    // If you want to remove the slash at the beginning you can use ltrim()
    $restOfURL = ltrim($restOfURL, "/");
?>

You can find more of the predefined server variables in the PHP documentation.

Update
Based on your comment to the question, I guess you're using something like mod_rewrite to rewrite the FOO, etc and route everything to just one file (index.php). In that case I would expect the rest of the URL to already be passed to the index.php file. However, if not, you can use mod_rewrite to pass the rest of the URL as a GET variable, and then just use that GET variable in your index.php file.

So if you enable mod_rewrite and then add something like this to your .htaccess file,

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

Then the rest of the URL will be available to you in your index.php file from the $_GET['url'] variable.

Rich Adams
+1  A: 

Reading $_SERVER['REQUEST_URI'], as everybody has pointed out, can tell you what the URL looks like, but it doesn't really work the way you want it unless you have a way to point requests for me.com/VALUE1 and me.com/VALUE2 to the script that will do the processing. (Otherwise your server will return a 404 error unless you have a script for each value you want, in which case the script already knows the value...)

Assuming you're using apache, you want to use mod_rewrite. You'll have to install and enable the module and then add some directives to your .htaccess, httpd.conf or virtual host config. This allows you make a request for me.com/XXX map internally to me.com/index.php?var=XXX, so you can read the value from $_GET['var'].

grossvogel
haha, it took me 18 minutes to write my response. Good pickup
Pablo
+1  A: 

Just by looking at the examples, i think you are looking for the apache mod_rewrite. You can apply a RewriteRule via an htaccess file, for example:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^([\w]+)$ /checkin.php?string=$1 [L]

For example this url http://foo.com/aka2 will be process by checkin.php script and will have "aka2" passed as $_GET['string'].

Make no mistake, the URL will still be visible in the browser as http://foo.com/aka2 but the server will actually process http://foo.com/checkin.php?string=aka

mod_rewrite documentation

Pablo
A: 

Why bother with all the fancy mod_rewrite/query_string business? There's already $_SERVER['PATH_INFO'] available for just such data.

Marc B