douyi2107 2017-06-05 09:54
浏览 22
已采纳

如何将GET参数显示为新目录?

I am currently working on a website where pupils can create articles that can be commented by others.

I have a already developed a script that creates them and stores them in a mysql db.

Every one of these articles should be available under www.mydomain/articles/xyz/

but I don't want to create a new directory for everyone. So is it possible to pass the parameter article=xyz (www.maydomain/articles/articles.php&articles=xyz) to be shown as before mentioned?

I am sorry if my problem is too complex. If you have a question regarding it, do not hesitate to contact me! :)

  • 写回答

2条回答 默认 最新

  • dongtan6206 2017-06-05 10:41
    关注

    Maybe you can do something like this with .htaccess file.
    You can redirect every page to your index.phppage

    <IfModule mod_rewrite.c>
        RewriteEngine On
        DirectoryIndex index.php
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
        RewriteRule . /index.php [L]
    </IfModule>
    

    And then you can handle your domain in your php file:

    <?php
    
    /*
    Example URL
    $url = www.maydomain/articles/querystring/articles/xyz/param2/value2/param3/value3
    $url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    */
    
    $url = "www.maydomain/articles/querystring/articles/xyz/param2/value2/param3/value3";
    $url = explode("/querystring/", $url);
    
    /*
    Where $url[0] is (www.maydomain/articles),
    and $url[1] is the rest of it (queryqtring/articles/xyz/param2/value2/param3/value3)
    */
    
    // If you need you can include your page articles on your index page like this
    $page_name = explode("/", $url[0]);
    $page_name = end($page_name);
    include("/path to your directory/". $page_name .".php");
    
    $query_string = $url[1];
    // And one more explode for query string:
    $query_params = explode("/", $query_string);
    
    
    for($i=0; $i<count($query_params); $i++)
    {
        // odd value is GET name
        $key = $query_params[$i];
        // even value is GET value
        $value = (isset($query_params[$i+1])) ? $query_params[$i+1] : "";
        $_GET[$key] = $value;
        $i++;
    }
    
    echo "<pre>";
    print_r($_GET);
    echo "</pre>";
    
    /*
    // GET Output
    Array
    (
        [articles] => xyz
        [param2] => value2
        [param3] => value3
    )
    */
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥30 数字信号处理实验报告
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改