分类 apache 下的文章

实际测试, 如果同一个端口有多个配置时, 和 ServerName 值无关, 如果请求进来的 ServerName 没有配置到, 按靠前加载的第一条虚拟主机设置配置为准, 不管 ServerName 名字是啥, 也不管里面的访问的域名是否在host中有配置, 都没有关系

Listen 80
<VirtualHost *:80>
        DocumentRoot "D:\code\www"
        FcgidInitialEnv PHPRC "D:/server/web/server/php"
        AddHandler fcgid-script .php
        FcgidWrapper "D:/server/web/server/php/php-cgi.exe" .php
      <Directory "D:\code\www">
          Options FollowSymLinks ExecCGI
          AllowOverride All
          Order allow,deny
          Allow from all
         Require all granted
      </Directory>
    ProxyPass /hb/ http://192.168.56.103:9501/
    ProxyPass /80/ http://127.0.0.1:81/
</VirtualHost>

业务场景说明

  1. 比如扒人家的站点, 图片相对地址一个个的扒很不方便, 通过301重定向简单配置一下可以实现当图片没有下载到对应的相对位置时, 会自动重定向下载
  2. vue容器环境下, 每次发布新版本时, 保证缓存老代码的用户访问不会出现404js文件, 可以考虑一台机器用于存放这部分的js文件

保存到文件 auto_download_image.php

<?php
$host301 = "https://static.zennioptical.com" ;
if ($_SERVER["REQUEST_URI"]=='/' || substr( $_SERVER["REQUEST_URI"] ,strripos($_SERVER["REQUEST_URI"] , "/") )==str_replace(".","",substr( $_SERVER["REQUEST_URI"] ,strripos($_SERVER["REQUEST_URI"] , "/") ))  ){
    header("HTTP/1.1 404 Forbidden");
    echo $_SERVER["REQUEST_URI"] ;
    echo "<hr><a href='https://test.templete.com/b/all-glasses/_/category.html'>https://test.templete.com/b/all-glasses/_/category.html</a> ";
    echo "<hr><a href='https://www.zennioptical.com" . $_SERVER["REQUEST_URI"] . "' target='_blank'>https://www.zennioptical.com" . $_SERVER["REQUEST_URI"] . "</a> ";
    exit ;
}
if ( stripos($_SERVER["REQUEST_URI"] , "?") ){
    $file_url = $host301. substr( $_SERVER["REQUEST_URI"] , 0 ,stripos($_SERVER["REQUEST_URI"] , "?") )   ;
}else{
    $file_url = $host301. $_SERVER["REQUEST_URI"] ;
}
function SaveFileFromUrl($link) {
    echo $link ;
    $file_link = str_replace("//" ,"" , $link) ;
    $file_link = substr($file_link , stripos($file_link,"/")+1 ) ;
    $folder_link = substr($file_link , 0 ,strripos($file_link,"/")+1) ;
    $ch = curl_init ($link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $img = curl_exec ($ch);
    curl_close ($ch);

//     $fp = fopen($image_name,'w'); // 下载图片到根目录

    if ( $img ){
        if (!is_dir($folder_link)) mkdir($folder_link , 0777 , true); // 如果不存在则创建
        $fp = fopen($file_link,'wb'); //下载图片到指定文件夹
        fwrite($fp, $img);
        fclose($fp);
//    header("HTTP/1.1 404 Forbidden");
        Header("HTTP/1.1 302 Moved Permanently");
        Header("Location: /" . $file_link );
    }else {
        echo "error" ;
    }


}
SaveFileFromUrl($file_url);

在静态文件目录下保存一个 .htaccess 文件

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ ../auto_download_image.php [L]
</IfModule>

RewriteEngine on
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# 防止目录浏览
Options All -Indexes