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条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起