douh9817 2013-01-12 12:22
浏览 11
已采纳

使用SERVER ['REQUEST_URI']和GET来控制网站流量

I have a small website. It's .htaccess file is like this:

RewriteEngine On
RewriteBase /site/
RewriteRule ^(.+)$ index.php [QSA,L]

So it redirects all the URLs to 'index.php'. I can get the requested URL and act accordingly :

$uri = $_SERVER['REQUEST_URI'];
switch($uri)
{
case 'index':
    LoadIndex();
    break;

case 'about':
    LoadAbout();
    break;

case 'Posts':
    LoadPosts();
    break;

default:
    LoadNotFound();
}

Say I want to use $_GET[] in Index page. That changes the URL, so it fails to load the page. How can I do that? How can I route my site without affecting $_GET[] variables in URLs?

  • 写回答

1条回答 默认 最新

  • doubi3929 2013-01-12 15:32
    关注

    $_SERVER[REQUEST_URI] will be /index.php and not index. $_SERVER[REQUEST_URI] also includes the QUERY_STRING. So, it might be /index.php?var1=abc&var2=def.

    If you need only the URI path, try PHP_SELF or SCRIPT_NAME. But keep in mind, that these will be /index.php too, including / and .php.

    $uri = $_SERVER['PHP_SELF'];
    switch($uri)
    {
    case '/index.php':
        LoadIndex();
        break;
    ...
    }
    

    You don't need QSA in your RewriteRule. From RewriteRule Directive

    Modifying the Query String
    By default, the query string is passed through unchanged.

    This means, the $_GET variable is available in your PHP script as before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决