PHP 重寫讀取檔案,適用多網域名稱的情況!

來源:互聯網
上載者:User

標籤:apache   seo   robots.txt   

如果一個網站同一套程式綁定了多個網域名稱 www.a.com ,www.b.com,為了區分他們的robots.txt、百度站長驗證檔案等,如 www.a.com/robos.txt 的內容來自於 /Virtual/www.a.com/robots.txt www.b.com/robots.txt 的內容來自於 /Virtual/www.b.com/robots.txt 的方法:
RewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^([a-zA-Z0-9_]{2,32}\.(xml|html|txt)) seo.php?file=$1 [L]

檔案 /seo.php
<?php/** * @param string $msg */function _404($msg = ‘‘){    header(‘HTTP/1.1 404 Not Found‘);    header(‘Status: 404 Not Found ‘);    // 確保FastCGI模式下正常    if (empty($msg)) $msg = ‘ 404 Not Found. ‘;    exit($msg);}function getExt($file){    return strrchr($file, ‘.‘);}$file = trim($_GET[‘file‘]);if (!preg_match(‘/^[a-zA-Z0-9_]{3,32}\.(html|txt|xml)$/i‘, $file)) _404();$dir = ‘Virtual/‘ . strtolower($_SERVER[‘HTTP_HOST‘]);if (!is_dir($dir)) _404();$filePath = $dir . ‘/‘ . $file;if (file_exists($filePath) && is_file($filePath)) {    switch (getExt($file)) {        case ‘.txt‘:            $type = ‘text/plain; charset=UTF-8‘;            break;        case ‘.xml‘:            $type = ‘application/xml‘;            break;        case ‘.html‘:        default:            $type = ‘text/html; charset=UTF-8‘;            break;    }    if (!empty($type)) {        header(‘Content-type:‘ . $type);    }    echo file_get_contents($filePath);} else {    _404();}

PHP 重寫讀取檔案,適用多網域名稱的情況!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.