0

Possible Duplicate:
How to: URL re-writing in PHP?

How can a website use an argument that you provide directly in the URL?

For example, you can visit:

http://isup.me/www.google.com

And then the scripts on that site can use the value www.google.com to see if the site is up.

I could do this with:

http://isup.me/index.php?url=ww.google.com

$url = $_GET['url'];

But that isn't very clean, and I would love to know how it is done the other way.

Thanks a lot! pimvdb pointed out that this was a duplicate of this question:

How to do URL re-writing in PHP?

Community
  • 1
  • 1
bag-man
  • 1,423
  • 2
  • 19
  • 23
  • Some frameworks provide more direct ways of doing it, but really what you've got there is pretty much the answer. You could maybe come up with a mini-framework of your own to split up the URL according to some consistent pattern, or whatever. – Pointy Dec 30 '11 at 15:19
  • use mod_rewrute in apache, or it equivalent in whatever httpd you're using. The data is still in $_GET but its format is hidden by the URL rewriting. That's how it's usually done. – GordonM Dec 30 '11 at 15:21
  • How would you make a form send users to a modified URL like that? Does it just always involve either Javascript or a redirect? – nkorth Dec 30 '11 at 16:22

3 Answers3

5

You need ModRewrite.

# Turn on URL rewriting
RewriteEngine On
RewriteBase /

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [L]
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
1

It all depends on your server config.

Everytime I've done this, I've used mod_rewrite for apache: (.htaccess file)

RewriteEngine On
RewriteBase /
#If not an existing File
RewriteCond %{REQUEST_FILENAME} !-f
#AND If not an existing Directory
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite to test.php
RewriteRule ^(.*) /test.php?site=$1 [L]

You can test this out using the php file:

<?php
print_r($_REQUEST);
Korvin Szanto
  • 4,531
  • 4
  • 19
  • 49
0

you can use mod_rewrite for that!

Example of a .htaccess File's content:

RewriteEngine On
RewriteCond %{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^([^/]*)$ index.php?get_var_name=$1
Alexander
  • 123
  • 3
  • 10