PHP根带的.htaccess [英] PHP root with .htaccess

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

问题描述

我使用的是000webhost的,它使用的public_html文件夹在根文件夹的根可见该网站。在该文件夹,我有一些PHP脚本,并与PHP索引页的其他文件夹的资产文件夹。使用规定/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

总之,如何让我所提到的code里面的 /的public_html / 点没有明确声明它(preferably作为htaccess的变化,我希望我的code才能够被移动到不同的主机,而不必重写任何东西)。

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.

在code:

require "/assets/includes/scriptname.php"

告诉PHP包括使用绝对路径的文件。使用硬codeD绝对路径,不推荐,因为当你把它移动到另一个目录或另一台服务器上有不同的路径上的code将无法正常工作。

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.

要指定一个包含文件的路径,最好的办法是产生它运行时,来自includer的路径开始。 PHP函数 目录名() 和恒 __ __ 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

比方说,你需要包括资产/ somescript.php 的index.php 。在写这篇文章的index.php

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

魔术常量 __ __ DIR 包含它被使用的文件的目录。对于的index.php __ __ DIR 设置为/(...路径来 - 您的用户目录...)/的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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆