$_GET and URL Rewriting for PHP

后端 未结 7 2119
旧时难觅i
旧时难觅i 2020-12-13 22:09

How does URL rewriting affect the $_GET parameter for PHP?

Say I have a URL like http://example.com/index.php?p=contact and I use $_G

相关标签:
7条回答
  • 2020-12-13 22:46

    In your case it wouldn't work. mod_rewrite, after it finds a match and rewrites http://example.com/index.php?p=contact to http://example.com/contact, does an internal redirect. Even after the redirect, the new, redirected URI, can still be matched against a condition and further redirected.

    In any case, incoming URIs aren't kept in memory, so not even Apache can reconstruct what the original URI was. PHP, by the time it's executed, also doesn't know the original URI. Hence, you loose your $_GET vars, as variables sent via GET are contained in the URL, which was, by now, transformed, and PHP populates the associative $_GET array by parsing incoming requests.

    Offering support for both would be painstaking. If you have http://domain.com/segment1/segment2/segment3 you have to associate the segments with something meaningful. You'd strip your domain and explode on '/', and in your case you could say that the first segment requests the page, and from http://example.com/contact/ you can extract page = 'contact'

    0 讨论(0)
提交回复
热议问题