【问题标题】:How do I make a subfolder act like a document root by htaccess?如何通过 htaccess 使子文件夹像文档根目录一样?
【发布时间】:2018-08-30 06:20:43
【问题描述】:

子站点位置:http://t.test.com/cooking/index.php

主站点位置:http://t.test.com/index.php

子网站加载大量带有绝对网址(例如/_upload/images/image.png)的图片,因此该网站丢失了图片链接。

我需要/_upload/images/image.png

http://t.test.com/cooking/_upload/images/image.png 而不是http://t.test.com/_upload/images/image.png

我还需要它不影响任何其他文件夹或根文件夹。这样我也可以访问http://t.test.com/_upload/images/image.png,如果我愿意的话。

我已经尝试了本文中的解决方案,但没有一个有效。 How do I make a subfolder act like a document root using htaccess?

RewriteCond %{REQUEST_URI} !^/~
RewriteRule ^(.*)$ cooking/$1 [L]

要么

RewriteRule ^cooking/(.*) /$1

有没有办法让它在被这个子文件夹请求重定向时?

我的 .htaccess

Options -Indexes

<IfModule rewrite_module>

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(css|includes|cache|js|siteadmin|images|_upload|mail|timthumb.php|scripts|styles|ajax_post.php|mail)($|/)  - [L]
    #RewriteCond $1 !^(css|includes|cache|js|siteadmin|images|_upload|mail|timthumb.php|scripts|styles) - [L]

    # Apache 2.4
    <IfModule authz_core_module>

        # Remove the extension
        RewriteCond %{REQUEST_URI} ^(.+)\.php$
        RewriteCond %{REQUEST_FILENAME} -f 
        RewriteRule .* %1 [QSA,R,L]

#----- Rewrite subfolder route-----
#RewriteCond %{REQUEST_URI} !^/~
#RewriteRule ^(.*)$ cooking/$1 [L]
#RewriteRule ^cooking/(.*) /$1

        # Point to the file
        RewriteCond %{REQUEST_URI} /[\w\-]+$
        RewriteCond %{REQUEST_FILENAME} !-d 
        RewriteCond %{REQUEST_FILENAME}\.php -f 
        RewriteCond %{REQUEST_URI} ^(.+)$
        RewriteRule .* %1.php [QSA,END]

    </IfModule>

    # Apache 2.2
    <IfModule !authz_core_module>

        # Point to the file
        RewriteCond %{REQUEST_URI} /[\w\-]+$
        RewriteCond %{REQUEST_FILENAME} !-d 
        RewriteCond %{REQUEST_FILENAME}\.php -f 
        RewriteCond %{REQUEST_URI} ^(.+)$
        RewriteRule .* %1.php [QSA,L]

    </IfModule>

</IfModule>

=========== index.php

$getSQL = "Select * From `foods`";

$tempData = $sql -> SelectDB($getSQL);
$thisData = FormatData($tempData['data']);

define('UPLOAD_IMG_PATH',"_upload/images/");

function FormatData( $data )
{

    foreach ($data as $key => $value) {

        foreach($value as $list_key =>$list_value){


            // separate pictures
            if (strstr($list_key,'pic') && !strstr($list_key,'_alt')) {
                $picArr = explode(",", $list_value);
                foreach ($picArr as $pic_key => $pic_value) {
                    if(!is_file(UPLOAD_IMG_PATH."/".$pic_value)){
                        unset($picArr[$pic_key]);
                    }
                    else{
                        $picArr[$pic_key] = '/'.UPLOAD_IMG_PATH.$picArr[$pic_key];

                    }

                    if(empty($picArr)){

                        $picArr[0]='styles/images/news/noimage.jpg';

                    }

                }
                $data[$key][$list_key] = $picArr;
            }


        }
    }
    return $data;
}

$tpl->assign('thisData', $thisData);
$tpl->display('index.html');

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    试试这个:

    RewriteRule ^_upload/images/   cooking%{REQUEST_URI} [L]
    

    因此,当 URI 以 /_upload/images/image.png 开头并在内部从真实路径 /cooking/_upload/images/image.png 获取时,您可以访问图像

    【讨论】:

    • 我得到了结果“styles/images/news/noimage.jpg”。我更新了我的问题。函数 FormatData 没有找到文件。
    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多