简体   繁体   English

如何在不影响URL的情况下将页面加载到其他目录中

[英]How to load up pages in different directory without it affecting URL

My file structure usually goes something like this - 我的文件结构通常是这样的-

/images/  
/css/  
init.php  
header.php  
page1.php  
page2.php 

etc ... 等...

After basically creating more and more pages, its becoming much more difficult to manage them because it's essentially turned the root directory into a massive list of pages. 在基本上创建了越来越多的页面之后,对其进行管理变得越来越困难,因为它实际上已将根目录变成了大量的页面列表。

How can I make it so the pages are placed in a folder along with its associated category like this - 我如何做到这一点,以便将页面及其相关类别放置在文件夹中,如下所示:

/pages/category1/page1.php  
/pages/category1/page2.php  
/pages/category2/page3.php   

without it affecting the URL so it still looks like http://domain.com/page1.php 不会影响URL,因此看起来仍然像http://domain.com/page1.php

Will editing the .htaccess be the most effective method of doing this? 编辑.htaccess是这样做的最有效方法吗? or is there another way? 还是有另一种方法?

I created a singular php file in the root directory that basically got a PHP page from a $_GET request and then checked to see if the PHP page exists. 我在根目录中创建了一个单一的php文件,该文件基本上是从$ _GET请求获得一个PHP页面的,然后检查该PHP页面是否存在。 If it did then I used REQUIRE_ONCE to get the page. 如果这样做,那么我使用REQUIRE_ONCE来获取页面。

//Get page from $_GET request 
if (isset($_GET['page'])){
  $page = $_GET['page'];
}

//Create path including the page
$url = ROOT_DIR . "/pages/".$page;

//If the page exists include it, if not then show a 404 page.
if(file_exists($url)){
  REQUIRE_ONCE $url;
}else{
  REQUIRE_ONCE "/pages/404.php";
}

This allows me to access login.php in the /pages/ directory using 这允许我使用以下命令访问/ pages /目录中的login.php

 http://domain.com/index.php?page=login.php

I then just rewrote the URL with .htaccess to make it more attractive. 然后,我只用.htaccess重写URL,使其更具吸引力。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1.php [QSA,L]

so that allows me to use 这样我就可以使用

 http://domain.com/login

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM