带有 .htaccess 的 PHP 根目录

时间:2023-02-27
本文介绍了带有 .htaccess 的 PHP 根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 000webhost,它使用根文件夹中的 public_html 文件夹作为站点的可见根.在那个文件夹中,我有一个包含一些 PHP 脚本的资产文件夹,以及包含 PHP 索引页面的其他文件夹.使用 require "/assets/includes/scriptname.php"; 不起作用,因为它试图找到 public_html 的同级文件夹.我可以编辑 .htaccess 以更改相对于 PHP 查找的根文件夹,但我不知道如何操作.

I'm using 000webhost, which uses a public_html folder in the root folder as the visible root for the site. In that folder, I have an assets folder with some PHP scripts, and other folders with PHP index pages. Using require "/assets/includes/scriptname.php"; does not work, as it tries to find a sibling folder to public_html. I'm allowed to edit .htaccess to change the root folder relative to the PHP lookup, but I don't know how.

文件树:

public_html (within root, but simulated root)
   folder1
      index.php
   folder2
      index.php
   folder3
      index.php
   assets
      subfolder
      subfolder
   index.php

简而言之,如何在 /public_html/ 中创建提到的代码点而不显式声明它(最好作为 .htaccess 更改,因为我希望我的代码能够移动到无需重写任何内容的不同主机).

In short, how do I make the mentioned code point inside /public_html/ without explicitly declaring it (preferably as a .htaccess change, as I want my code to be able to be moved to a different host without rewriting anything).

对于 .htaccess 重写的答案,您能解释一下它的每一行是如何工作的吗?谢谢.

For an answer with the .htaccess rewrite, could you explain how each line of it works? Thanks.

推荐答案

.htaccess 对您没有帮助.它是一个 Apache 配置,它不会以任何方式影响 PHP 的行为.

.htaccess does not help you. It is an Apache configuration and it does not affect the behavior of PHP in any way.

代码:

require "/assets/includes/scriptname.php"

告诉 PHP 使用绝对路径包含文件.不推荐使用硬编码的绝对路径,因为当您将其移动到另一个目录或具有不同路径的不同服务器时,代码将不起作用.

tells PHP to include a file using an absolute path. Using hardcoded absolute paths is not recommended because the code won't work when you move it into another directory or on a different server that has different paths.

指定包含文件路径的最佳方法是在运行时生成它,从包含程序的路径开始.PHP 函数 dirname() 和常量 __DIR__ 是这里的帮手.

The best way to specify the path of an included file is to generate it runtime, starting from the path of the includer. The PHP function dirname() and the constant __DIR__ are the helpers here.

给定示例文件结构:

public_html
   |
   +- index.php
   |
   +- assets
   |     |
   |     +- somescript.php
   |
   +- includes
         |
         +- header.php
         |
         +- footer.php

假设您需要在 index.php 中包含 assets/somescript.php.在 index.php 中写入:

Let's say you need to include assets/somescript.php in index.php. Write this in index.php:

 require __DIR__.'/assets/somescript.php';

魔术常量 __DIR__ 包含使用它的文件的目录.对于 index.php__DIR__ 设置为 '/(...path-to-your-user-directory...)/public_html'.

The magic constant __DIR__ contains the directory of the file where it is used. For index.php, __DIR__ is set to '/(...path-to-your-user-directory...)/public_html'.

如果 somescript.php 需要包含 header.php 它应该这样做:

If somescript.php needs to include header.php it should do it like this:

require dirname(__DIR__).'/includes/header.php';

等等.

通过这种方式,您可以将整个应用程序移动到不同的目录、不同的服务器甚至不同的操作系统上,它仍然可以工作.

This way you can move the entire application to a different directory, on a different server and even on a different OS and it will still work.

这篇关于带有 .htaccess 的 PHP 根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!