我加入了 Sjoerd 的说法,但是有很多方法可以让你随心所欲地重写你的 url!
Apache 和(IIS 也是)支持这样的 url-s:http://example.com/index.php/my-rewritten-url_62
function URISegment($segment)
{
$uri_array = explode('/',$_SERVER['REQUEST_URI']);
$uri_count = count($uri_array);
$returning_uri = array();
for($i = 0;$i<$uri_count;$i++)
{
if(empty($uri_array[$i]) || $uri_array[$i] == "index.php")
unset($uri_array[$i]);
else
array_push($returning_uri,$uri_array[$i]);
}
if($segment < count($returning_uri))
return $returning_uri[$segment];
else
return false;
}
这可行,但您也需要定义基本 url,这需要在文件开头调用,并在每个图像、脚本等调用时实现。
function BaseURL()
{
if(isset($_SERVER['HTTP_HOST']))
{
$base = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
$base .= '://'. $_SERVER['HTTP_HOST'];
$base .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}
else
{
$base = 'http://localhost/';
}
return $base;
}
在这之后你可以用这个来代替:
// http://example.com/?MyKey=Some-data
$MyKey = $_GET['MyKey']; //which is the first item
echo $MyKey;
// results: Some-data
这个:
// http://example.com/?MyKey=Some-data
$MyKey = URISegment(0);
echo $MyKey;
// results: Some-data
每个人都得到相同的结果。
PS:
我喜欢这个解决方案,因为我可以根据需要混合 url 类型:
example.com/index.php/index/evaled-article?some=db-stored&code=snipplet
当然你也可以像 FRKT 说的那样重写你的 url :)
当然,如果你想隐藏index.php你需要使用mod_rewrite,因为没有办法