[英]PHP absolute relative URL issue during site migration
I am migrating my PHP files to a new server. 我正在将PHP文件迁移到新服务器。 On the old server, I have used absolute URLs in my
require_once
and include_once
directives. 在旧服务器上,我在我的
require_once
和include_once
指令中使用了绝对URL。 To be clearer, the absolute URLs specify the location from where index.html
is stored. 为了更清楚,绝对URL指定了
index.html
的存储位置。
However, the new server requires relative URLs. 但是,新服务器需要相对URL。 So if
A.php
requires to include B.php
, the relative path to B.php
from A
must be specified. 因此,如果
A.php
要求包含B.php
,则必须指定从A
到B.php
的相对路径。
Is there a setting in php.ini
which enforces the PHP interpreter to accept absolute URLs? php.ini
是否有设置可强制PHP解释器接受绝对URL?
No there is not. 不,那里没有。 If You set path to
var/www/something.php
it is relative, if You set path to /var/www/something.php
(or C:\\\\var\\\\www\\\\something.php
in windows) it is absolute 如果将路径设置为
var/www/something.php
是相对的,则如果将路径设置为/var/www/something.php
(或Windows中的C:\\\\var\\\\www\\\\something.php
),则是绝对的
There is a way of configuring your new server to accept your URLs as relative to index.html
. 有一种方法可以配置新服务器以接受相对于
index.html
URL。
You are looking for a directive include_path
in your php.ini
. 您正在
php.ini
中寻找指令include_path
。 The solution is to create php.ini file in the same directory with index.html
(in the root directory of your site) and set: 解决方案是在与
index.html
相同的目录中(在网站的根目录中)创建php.ini文件并设置:
include_path="/path/to/your/directory"
Then, all the include
and require
instructions will try to search the file in the specified directory first. 然后,所有
include
和require
说明将首先尝试在指定目录中搜索文件。
Here is the documentation for this directive: http://php.net/manual/en/ini.core.php#ini.include-path 这是此指令的文档: http : //php.net/manual/zh/ini.core.php#ini.include-path
Note that not all hosting providers will let you use your own php.ini
file. 请注意,并非所有托管服务提供商都将允许您使用自己的
php.ini
文件。 I mean, sometimes that new php.ini
is just ignored. 我的意思是,有时只是忽略了新的
php.ini
。 Then, you will need to find a way of changing the original php.ini
然后,您将需要找到一种更改原始
php.ini
Other way is using $_SERVER['DOCUMENT_ROOT']
constant: 另一种方法是使用
$_SERVER['DOCUMENT_ROOT']
常量:
include_once $_SERVER['DOCUMENT_ROOT'] . '/file.php';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.