views:

62

answers:

4

To request some data from a web server, we can use the GET method,like

www.example.com/?id=xyz

but I want to request the data like

www.example.com/xyz

How can it be achieved in PHP?

+3  A: 

Create a file in your root directory and call it .htaccess. Put this in it:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [R=301,L]

If someone goes to www.example.com/xyz and xyz is not a directory or a file it will load /index.php?xyz instead. It will be completely transparent to your users.

John Conde
could you please explain in detail?
appusajeev
The first line checks to see if xyz is a file. If it is the file will load normally. If not the second lne checks to see if xyz is a directory. If it is it will load normally. If it isn't it will pass xyz as a GET parameter to index.php.
John Conde
+1  A: 

You could use mod-rewrite, some more info is here

http://www.trap17.com/index.php/php-mod-rewrite-tutorial_t10219.html

John Boker
+1  A: 

I'm not sure "posting" the data is the right terminology, but you can use Apache mod_rewrite to make URLs like '/xyz' direct to your PHP application. For example, place a .htaccess file in your web root with the following,

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Now the URL specified is available in $_GET['url].

Stephen Melrose
i did not mean POST by 'posting' in the question.
appusajeev
A: 

I don't think you can achieve what you want with the GET method, as PHP will always append form data in a query string to the end of the URL specified in your form's action attribute.

Your best bet is to post the data to a handler (i.e. www.example.com/search) and then use that page to redirect to the correct page.

So if you entered a query for hello+world, that variable would be passed to your /search page and processed by the PHP script to re-direct to /hello+world.

Of course, you're going to need the correct .htaccess rules in place to handle searches like this, as well as sanitizing data.

Martin Bean
what i want is i want to replace www.example.com/id=xyz with www.example.com/xyz ,ie i want to type the latter url in the browser and get the result rather than the first method.
appusajeev
Should just be a case of a `mod_rewrite` rule if you're hand-typing URLs. Chances are I've read the original post wrong.
Martin Bean