-3

Possible Duplicate:
getting current URL

I wan't to use url (with any GET parameters, etc) in PHP code.

So when my website is on url: http:/sample.com/art/id/title I want to display this string in website.

There is also one thing... My code would be fired up on various servers with various url conventions.

Community
  • 1
  • 1
pawel-kuznik
  • 437
  • 4
  • 11

3 Answers3

3

The following should give you both the domain and URL:

$_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
Mike Fahy
  • 5,487
  • 4
  • 24
  • 28
2
// Get HTTP/HTTPS (the possible values for this vary from server to server)
$myUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] && !in_array(strtolower($_SERVER['HTTPS']),array('off','no'))) ? 'https' : 'http';
// Get domain portion
$myUrl .= '://'.$_SERVER['HTTP_HOST'];
// Get path to script
$myUrl .= $_SERVER['REQUEST_URI'];
// Add path info, if any
if (!empty($_SERVER['PATH_INFO'])) $myUrl .= $_SERVER['PATH_INFO'];
// Add query string, if any (some servers include a ?, some don't)
if (!empty($_SERVER['QUERY_STRING'])) $myUrl .= '?'.ltrim($_SERVER['REQUEST_URI'],'?');

echo $myUrl;

Should account for just about any variations you might encounter (although mod_rewrite may screw it up in an un-correctable way, I'm not sure).

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
0

You have to have an .htaccess file, it's called URL re-write, an example .htaccess file:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php?query=$1&%{QUERY_STRING}

This will send everything like art/id/title to your index.php page and will be usable using $_GET['query']. So in the index.php file, you can use the variable like so:

echo $_GET['query'];
jValdron
  • 3,408
  • 1
  • 28
  • 44