【问题标题】:How to remove directory from the URL如何从 URL 中删除目录
【发布时间】:2019-04-06 06:51:16
【问题描述】:

我有这个文件结构:

  • 新的

    • 前端

      • index.php
    • 后端

    • index.php

如果可以,我可以让前端和后端文件夹隐藏起来

localhost/new/frontend/index.php

将变成:

localhost/new/

【问题讨论】:

    标签: php apache .htaccess url mod-rewrite


    【解决方案1】:

    我确信 MVC 将解决您当前的问题。所以,我们应该按照MVC标准来实现网站。

    一些例子:

    https://medium.com/@noufel.gouirhate/create-your-own-mvc-framework-in-php-af7bd1f0ca19

    https://github.com/DawidYerginyan/simple-php-mvc

    【讨论】:

      【解决方案2】:

      如果您使用原生 PHP 编写它,那么您必须编写自己的路由系统。像这样的:

      $str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
      $sections = explode("/", $str, 3);
      
      $page = (isset($sections[2]) && $sections[2] != '') ? $sections[2] : 'homepage';
      $page = parse_url($page);
      $page = trim($page['path'], '/');
      
      // list of your custom php function
      $functions = array(
          'contact-us-success' => 'contact_us_success',
      );
      
      //check functions first
      if (isset($functions[$page])) 
      {
          call_user_func($functions[$page]);
          return true;
      } 
      
      //else check page
      elseif (is_file("pages/{$page}.php")) 
      {
          include_once("pages/template/header.php");
          include_once("pages/{$page}.php");
          include_once("pages/template/footer.php");
          return true;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-11
        • 1970-01-01
        • 2019-10-03
        • 1970-01-01
        • 2011-05-15
        • 2021-08-29
        • 2011-09-24
        • 2012-03-19
        相关资源
        最近更新 更多