停止干净的URL影响页面HTML中的相对路径

我在.htaccess中使用以下内容实现了干净的URL

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

在我的index.php中, $_SERVER['PATH_INFO']被解析以确定请求的页面和任何参数。

这一切工作正常,但任何请求一个网址与多个部分,即

www.example.com/page/foo/bar

导致页面上的所有链接资源(css,js,图像)和超链接因其使用相对路径而中断。 具有隐含域的绝对路径(也就是将所有路径预先加上“/”)是不可行的,因为除了它自己的根URL之外,该站点必须可以作为父网站的子目录访问和正常工作。 我无法硬编码所有链接的完全限定的绝对路径,因为我无法控制页面上的某些内容。 使用<base>标签也是不可能的。

我在这里还有其他选择吗?


对PHP的每个请求都落在同一个index.php文件中吗? 在这种情况下,文件本身知道它在哪里,并且可以计算它所在的目录。

这不是最好的方法,但它可能会给你一些想法。

define('DR', $_SERVER['DOCUMENT_ROOT');
define('BD', str_replace(DR, '', dirname($_SERVER['SCRIPT_FILENAME'] . '/'); // base dir

然后预先安排与BD所有链接。


当你创建一个php路由器时,做你通常做的事情......为资产和URL创建一个链接生成工具。

由于您使用的是前端控制器,因此您可以根据配置预先定义所有目录,然后生成如下所示的路径:

function asset_url($filename, $type = 'img') {
   global $config;
   if(isset($config['paths'][$type])) {
      return $config['paths'][$type] . '/' . $filename;
   }
}

通常,id使用注册表模式实现注册此配置,而不是使用global但我不知道你的设置,所以这只是一个简单的例子。


这是一个棘手的解决方案。 最简单的事情是向应用程序引入baseUrl参数。 如果该网站在根目录下运行,则该网站将为空,并且会类似/corp/sitedomain.com/corp/site运行。

这样,当你显示URL时,你可以使用<?php echo $app_base_url . '/css/file.css' ?> <?php echo $app_base_url . '/css/file.css' ?>

我使用Zend Framework很多,他们有一个名为Zend_Controller_Request_Http的类,它有一个名为getBaseUrl的方法,通过一些复杂的逻辑检查有关脚本路径和请求文件的各种$_SERVER变量,最终找出您的基本Url实际上是什么。

如果您使用了特定的Zend Framework,您可以这样做:

$request = new Zend_Controller_Request_Http();
$baseUrl = $request->getBaseUrl();

由于Zend Framework是模块化的,因此您不需要包含整个库,我之前只使用过这个类(及其依赖关系),因此我可以轻松计算小型应用程序中的基本URL,而无需使用整个ZF库。

为了给你一个确定这个逻辑的概念,下面是执行大部分工作的代码(注意,不要直接使用它,而应该使用Zend框架类,如上所示)。

/**
 * Set the base URL of the request; i.e., the segment leading to the script name
 *
 * E.g.:
 * - /admin
 * - /myapp
 * - /subdir/index.php
 *
 * Do not use the full URI when providing the base. The following are
 * examples of what not to use:
 * - http://example.com/admin (should be just /admin)
 * - http://example.com/subdir/index.php (should be just /subdir/index.php)
 *
 * If no $baseUrl is provided, attempts to determine the base URL from the
 * environment, using SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF, and
 * ORIG_SCRIPT_NAME in its determination.
 *
 * @param mixed $baseUrl
 * @return Zend_Controller_Request_Http
 */
public function setBaseUrl($baseUrl = null)
{
    if ((null !== $baseUrl) && !is_string($baseUrl)) {
        return $this;
    }

    if ($baseUrl === null) {
        $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : '';

        if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
            $baseUrl = $_SERVER['SCRIPT_NAME'];
        } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
            $baseUrl = $_SERVER['PHP_SELF'];
        } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
            $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
        } else {
            // Backtrack up the script_filename to find the portion matching
            // php_self
            $path    = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
            $file    = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
            $segs    = explode('/', trim($file, '/'));
            $segs    = array_reverse($segs);
            $index   = 0;
            $last    = count($segs);
            $baseUrl = '';
            do {
                $seg     = $segs[$index];
                $baseUrl = '/' . $seg . $baseUrl;
                ++$index;
            } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
        }

        // Does the baseUrl have anything in common with the request_uri?
        $requestUri = $this->getRequestUri();

        if (0 === strpos($requestUri, $baseUrl)) {
            // full $baseUrl matches
            $this->_baseUrl = $baseUrl;
            return $this;
        }

        if (0 === strpos($requestUri, dirname($baseUrl))) {
            // directory portion of $baseUrl matches
            $this->_baseUrl = rtrim(dirname($baseUrl), '/');
            return $this;
        }

        $truncatedRequestUri = $requestUri;
        if (($pos = strpos($requestUri, '?')) !== false) {
            $truncatedRequestUri = substr($requestUri, 0, $pos);
        }

        $basename = basename($baseUrl);
        if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
            // no match whatsoever; set it blank
            $this->_baseUrl = '';
            return $this;
        }

        // If using mod_rewrite or ISAPI_Rewrite strip the script filename
        // out of baseUrl. $pos !== 0 makes sure it is not matching a value
        // from PATH_INFO or QUERY_STRING
        if ((strlen($requestUri) >= strlen($baseUrl))
            && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
        {
            $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
        }
    }

    $this->_baseUrl = rtrim($baseUrl, '/');
    return $this;
}
链接地址: http://www.djcxy.com/p/34853.html

上一篇: Stop clean URLs from affecting relative paths in page HTML

下一篇: Convert absolute path into relative path given a current directory using Bash