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.exa开发者_Go百科mple.com/xyz
How can it be achieved in PHP?
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.
You could use mod-rewrite, some more info is here
http://www.trap17.com/index.php/php-mod-rewrite-tutorial_t10219.html
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]
.
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.
精彩评论