首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP Url路由分离目录

问PHP Url路由分离目录
EN

Stack Overflow用户
提问于 2016-01-12 17:53:03
回答 2查看 30关注 0票数 0

我正在为我的网站寻找PHP路由器,并且我的文件在根目录中;例如: index.php、details.php、signup.php、recover.php

因此,我希望details.php、signup.php和recover.php位于一个名为pages的目录中,然后访问mysite.com/details或Mysite.com/?page=details上的链接。我真的想把它们放在一个单独的文件夹而不是根目录下。

谢谢大家!

EN

回答 2

Stack Overflow用户

发布于 2016-01-12 17:58:40

你只是想要一个MVC。我强烈建议您遵循本系列教程。没有使用任何框架,但仍在工作。

https://www.youtube.com/watch?v=OsCTzGASImQ&list=PLfdtiltiRHWGXVHXX09fxXDi-DqInchFD

票数 0
EN

Stack Overflow用户

发布于 2016-01-12 20:24:49

我几年前写了一个脚本,我认为它可能很适合您的需求。

备注:

  • 脚本由顶部的模式位置控制。

我对它进行了修改(只是在模式中插入了pages/ ),希望能适合你的情况。

  • 你可以用这个脚本替换你想要移动到目录页面的所有文件--不需要任何URI脚本自动处理大多数URI元素,包括端口号。但是:它不处理登录凭证(用户名和密码),因为许多现代浏览器和服务器不尊重这些。

  • ,这个脚本也是受版权保护的,它是免费的。因此您可以在非商业性和商业性environments.

中使用它

代码

代码语言:javascript
复制
    <?php
    /*
     ---------------------------------------------------------------------------
     Redirector Script
     Copyright (C) 2008, methodica.ch, http://www.methodica.ch
     Purpose: Redirect GET request according the pattern 'location'
     Pattern variables (embraced in curly brackets):
       prot: Protocol (i.e. http | https)
       host: Hostname (e.g. www.example.org)
       port: Portnumber (80 | 443 | 81)
       path: Path to the file (includes trailing '/', might be empty)
       file: Filename
       quer: Query, GET parameters (e.g. 'id=myId&mode=1&close=automatic')
     Other text is taken literally
     ---------------------------------------------------------------------------
    */
    define('location','{prot}://{host}{port}/{path}pages/{file}{quer}');

    // Do not modify the code below! -------------------------------------------

    // Get URI elements
    $prot = ((isset($_SERVER['HTTPS'])) && (strtoupper($_SERVER['HTTPS']) == 'ON')) ? 'https' : 'http';
    $server_name = getenv('SERVER_NAME');
    $script_url  = $prot . '://' . $server_name . ':' . getenv('SERVER_PORT') . $_SERVER['PHP_SELF'];
    $xres = preg_match_all('%^(http[s]{0,1})://(.*?):[\d]*?/(.*/)*(.*)$%i', $script_url, $res, PREG_PATTERN_ORDER);

    if (($xres!==false) && ($xres>0)) {
        // Prepare variables
        $prot = $res[1];
        $host = $res[2];
        $path = $res[3];
        $file = $res[4];
        $quer = $_SERVER['QUERY_STRING'];
        $xport = getenv('SERVER_PORT');
        $port = ($prot=='https') ? ( ($xport=='443') ? '' : ":$xport" ) : ( ($xport=='80') ? '' : ":$xport" );
        $location = location;
        // Replace variable references in pattern
        preg_match_all('/\{.*?\}/i', $location, $res, PREG_PATTERN_ORDER);
        for ($i = 0; $i < count($res[0]); $i++) {
            $varname = str_replace(array('{','}'),'',$res[0][$i]);
            $srce = $res[0][$i];
            $dest = $$varname;
            if (is_array($dest)) $dest = ($dest[0]);
            if ($srce=='{quer}') $dest = '?'.$dest; 
            $location = str_replace($srce,$dest,$location);
        }
        // Send redirection header
        header("Location: $location");
        exit();
    } else {
        // Something went awfully wrong
        echo "ERROR: Cannot parse URI:<br />'$script_url'";
        exit();
    }

    ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34740323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档