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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度