简体   繁体   中英

How to take PHP input parameter from URL like this www.domain.com/item/ABC123

I the want user to visit www.domain.com/item/ABC123


As you can see here ABC123 should be the input parameter

like this www.domain.com/item/?Id=ABC123

Under /item/ there is an index.php file, that will take an Id input parameter, how do I pass the input parameter value from www.domain.com/item/ABC123 to the id input parameter of the index.php file?

using .htaccess file

add rewrite rule:

RewriteRule ^/item/([a-zA-Z0-9-]*)$ /item/index.php?id=$1 [QSA,L,E]

You'll need url rewriting. Basicaly, you'll have an .htaccess with a rewrite rule which will transforms your URL. For instance, www.domain.com/item/some/things/ABC123 into www.domain.com/item/index.php?queryString=some/things/ABC123 and your PHP will do the rest.

Something similar to this should work:

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php

index.php

$uri = preg_replace("#(/+)#","/",$_SERVER["REQUEST_URI"]);
$uri = array_shift(explode("/",$uri));

now, $uri is an array of each portion of the URI. so /a/b/c becomes:

array(
    0 => "a",
    1 => "b",
    2 => "c",
)

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