繁体   English   中英

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

[英]How do I make a subfolder act like a document root by htaccess?

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

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

子站点加载了大量带有绝对URL的图像,例如/_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

我已经尝试了本文中的解决方案,但没有一个起作用。 如何使用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');

尝试这个 :

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM