简体   繁体   中英

PHP get QUERY_STRING after '/'

I have my domain name and I would like to get the query from a URL like this: http://domain.com/querystring and output would then be: querystring

It is the same technique bit.ly uses and Facebook for profile pages. Do I have to do something in my .htaccess file? or is there a normal PHP command to do so.. I think it would some kind of .htaccess URL rewriting, but I would just know if there is any other option.

For the Facebook example : www.facebook.com/profile.php?id=####

then uses mod_rewrite to look like www.facebook.com/username

but to get the username the code is :

$_GET['id'];

Facebook has references to the id and usernames in its db tables

I assume you're using a web-server, like Apache and index.php is your controller.

Enable mod_rewrite , then config an .htaccess file in the root directory (or your vhost definition).

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Then use $_SERVER['QUERY_STRING']

Check the $_SERVER superglobal.

If you simply want to use part of the URI for your query, you can get the entire URI from the $_SERVER superglobal like this, then break it apart to get whatever piece you might need

$uri = $_SERVER['REQUEST_URI'];
$pieces = explode("/", $uri);
$myQuery = $pieces[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM