在子目录上运行Tumblr几乎成功

时间:2011-07-31 08:37:58

标签: .htaccess redirect tumblr

我一直在研究一个小代理,从子目录运行Tumblr博客。我已经能够将Tumblog掩盖到example.com/blog/,但我无法关注博客中的任何链接。我是一名设计师,并且花了很多时间来研究这个问题。对不起,如果这听起来很愚蠢。我的问题是为什么博客中的链接不起作用?

这是我的.htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog\/?$ proxy.php?url=http://seo-services-greece.tumblr.com/$1?%

<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresByType image/gif A2592000
    ExpiresByType image/jpeg A2592000
    ExpiresByType image/png A2592000
    ExpiresByType text/css A2592000
    ExpiresByType application/javascript A2592000
    #ExpiresDefault A2592000
</IfModule>

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE text/ecmascript
AddOutputFilterByType DEFLATE application/ecmascript
AddOutputFilterByType DEFLATE text/jscript

Header unset ETag
FileETag None

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^seoprojects.gr [nc]
rewriterule ^(.*)$ http://www.seoprojects.gr/$1 [r=301,nc]

这是我的proxy.php:

<?php 

$from = "seo-services-greece.tumblr.com";
$unto = "seoprojects.gr/blog";

// Because Dreamhost doesn't do remote fopens, and to get content-type
function fetch($url) {
    $curl = curl_init();
    $timeout = 5; // set to zero for no timeout
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $html = curl_exec($curl);
    $content_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
    curl_close($curl);
    return array($html, $content_type);
}

list($html, $content_type) = fetch($_GET['url']);

// Fix root-relative links etc.
$html = preg_replace('/\b(href|src|rel)="\//', '$1="http://'.$unto.'/', $html);

// Fix the iframe-url
$html = str_replace("iframe?src=http://".$unto, "iframe?src=http://".$from, $html);

// fix audioplayer-url
$html = str_replace("audio_file=http://".$unto, "audio_file=http://".$from, $html);

// Replace the old URL with the new
$html = str_replace($from, $unto, $html);

header("Content-type: $content_type");
echo $html;

?>

1 个答案:

答案 0 :(得分:1)

您的重写规则(模式)不正确 - 您有后引用$1但没有“catch group”将用于$1。请改用这些行:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/?$ proxy.php?url=http://seo-services-greece.tumblr.com/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.+)$ proxy.php?url=http://seo-services-greece.tumblr.com/$1 [L]

第一条规则将处理/blog/blog/,而第二条则适用于/blog/something

不幸的是,任何查询字符串都将丢失(例如/blog/?ref=yahoo)。要将其传递给proxy.php,请尝试在目标网址末尾添加?%{QUERY_STRING}:例如proxy.php?url=http://seo-services-greece.tumblr.com/$1?%{QUERY_STRING} - 我只是不保证这会像那样 - 它可能需要一些转义。


<强>更新 你有2个重写规则块 - 一个在最顶层,一个在.htaccess的最后。您需要从底部移动那些顶部并将它们放在之前 proxy.php规则:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# force www. in domain name
RewriteCond %{HTTP_HOST} ^seoprojects.gr [NC]
RewriteRule ^(.*)$ http://www.seoprojects.gr/$1 [R=301,L]

# thumblr proxy rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/?$ proxy.php?url=http://seo-services-greece.tumblr.com/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.+)$ proxy.php?url=http://seo-services-greece.tumblr.com/$1 [L]