我在使用我编写的这个PHP 函数获取根 URL时遇到问题。我可以得到它,但我必须为每个服务器设置例外,但我想要一个一致的功能。你能告诉我如何解决它吗?
我把它放在给定网站的根文件夹中,在我的 Ubuntu 工作站上,它位于 /var/www/website 下,但在我的 CentOS 服务器上,它可能位于 /home/me/website 下。它可以在我的工作站上运行,但在服务器上由于某种原因我必须制作不同的版本。此外,我还必须为另一个似乎也不喜欢该功能的 CentOS 服务器制作另一个版本。在这个问题的结尾,我还列出了一些背景信息,说明我为什么要这样做。
工作站版本
public function getRootURL() {
$sTemp = str_replace($_SERVER['DOCUMENT_ROOT'],'',__DIR__);
$sTemp = str_replace($sTemp, '', $_SERVER['REQUEST_URI']);
$sPageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' . $_SERVER['SERVER_NAME'] : 'http://' . $_SERVER['SERVER_NAME'];
$sPageURL .= ($_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
$sRootURL = str_replace($sTemp,'',$sPageURL);
$sRootURL = rtrim($sRootURL, '/') . '/';
$sRootURL = (strpos(' ' . $sRootURL, '://') === FALSE) ? $sPageURL : $sRootURL;
return $sRootURL;
}
服务器版本
public function getRootURL() {
$sTemp = str_replace($_SERVER['DOCUMENT_ROOT'],'',__DIR__);
$sTemp = str_replace($sTemp, '', $_SERVER['REQUEST_URI']);
$sPageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' . $_SERVER['SERVER_NAME'] : 'http://' . $_SERVER['SERVER_NAME'];
$sPageURL .= ($_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
$sRootURL = str_replace($sTemp,'',$sPageURL);
$sRootURL = rtrim($sRootURL, '/') . '/';
$sRootURL = (strpos(' ' . $sRootURL, '://') === FALSE) ? $sPageURL : $sRootURL;
if (strpos(' ' . $sRootURL, 'mycentosserver.com')>0) {
$sRootURL .= 'website/';
}
return $sRootURL;
}
在服务器版本上,请注意“mycentosserver.com” if/then 语句——这就是区别。
备用服务器版本
function getRootURL() {
$sTemp = str_replace($_SERVER['DOCUMENT_ROOT'],'',__DIR__);
$sTemp = str_replace($sTemp, '', $_SERVER['REQUEST_URI']);
$sPageURL = (@$_SERVER['HTTPS'] == 'on') ? 'https://' . $_SERVER['SERVER_NAME'] : 'http://' . $_SERVER['SERVER_NAME'];
$sPageURL .= ($_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'] : $_SERVER['REQUEST_URI'];
$sRootURL = str_replace($sTemp,'',$sPageURL);
$sRootURL = rtrim($sRootURL, '/') . '/';
$sRootURL = (strpos($sRootURL, '://') === FALSE) ? $sPageURL : $sRootURL;
$sRootURL .= 'website2/'; // heck, I give up -- strange Apache problem on this server
return $sRootURL;
}
背景
通常我会使用 MVC 框架,但我有一个客户想在我离开时编辑东西,并且不喜欢 MVC 并且不理解它。他具有简单的 PHP 技能。所以,我使用一个看起来像这样的 .htaccess 来处理漂亮的 URL,以及那些在 URL 末尾可以出现斜杠的 URL,或者:
Options -Indexes +FollowSymlinks -MultiViews
RewriteEngine on
#No slash on the end of the url and not a real file/folder? then show x.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*$ "$0.php" [nc]
#slashes on the end still and not a real file/folder, then remove them
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
#if still here, and not a real file/folder, then show x.php in the alternative way
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ "$1.php" [nc]
并且正在创建像_css、_js、_classes 等子文件夹——试图让事情变得相当简单。每个页面在顶部都有 PHP,然后在 HTML 的底部有参数。这是为了尝试消除意大利面条式代码。客户端似乎喜欢这样,尽管我更喜欢 MVC,尤其是出于这个根 URL 的原因。
但是请注意,使用 .htaccess 技术,在某些情况下,我的 HTML 可能没有正确的相对路径,例如有人在 URL 的末尾添加斜杠,因此我必须在 HTML 中使用标签来清除相对路径寻路。这就是 getRootURL() 函数必须正常工作的原因。