如何通过htaccess使子文件夹像文档根一样?

时间:2018-03-21 08:10:15

标签: php .htaccess

子网站位置: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');

1 个答案:

答案 0 :(得分:0)

试试这个:

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

因此,当URI以/_upload/images/image.png开头并且内部来自真实路径/cooking/_upload/images/image.png

时,您可以访问图片