【问题标题】:.htaccess - Configuring an Apache server for Memento.htaccess - 为 Memento 配置 Apache 服务器
【发布时间】:2013-12-31 00:44:18
【问题描述】:

我尝试配置一个Apache 服务器以添加一个指向Memento TimeGates url 的HTTP Link header

我的 htaccess:

RewriteEngine on

RewriteCond %{IS_SUBREQ} false
RewriteRule ^/(.*) - [E=ORIGURI:%{HTTP_HOST}/$1]

RewriteRule ^/(.*) - [E=ORIGQRY:]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^/(.*) - [E=ORIGQRY:?%{QUERY_STRING}]

RewriteRule ^/(.*) - [E=ORIGPROTO:http]
RewriteCond %{HTTPS} on
RewriteRule ^/(.*) - [E=ORIGPROTO:https]

Header always set Link 
    "<http://purl.org/memento/timegate/%{ORIGPROTO}e://%{ORIGURI}e%{ORIGQRY}e>;rel=timegate"

.
.
.

原为:http://www.mementoweb.org/tools/apache/

我正在XAMPP 服务器上测试代码,但服务器发送的响应是:

.
.
.
Link    <http://purl.org/memento/timegate/(null)://(null)(null)>;rel=timegate
Server  Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
.
.
.

htaccess 出了什么问题?

编辑 1

按照 Jon Lin 的建议删除前导斜杠:

RewriteEngine On
RewriteCond %{IS_SUBREQ} FALSE
RewriteRule ^(.*) - [E=ORIGURI:$1]
RewriteRule ^(.*) - [E=ORIGQRY:]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*) - [E=ORIGQRY:?%{QUERY_STRING}]
RewriteRule ^(.*) - [E=ORIGPROTO:http]
RewriteCond %{HTTPS} on
RewriteRule ^(.*) - [E=ORIGPROTO:https]
Header always set Link "<http://purl.org/memento/timegate/%{ORIGPROTO}e://%{ORIGURI}e%{ORIGQRY}e>;rel=timegate"

服务器发送的新响应:

Link <http://purl.org/memento/timegate/http://(null)>;rel=timegate

正如我们所见,protocole 已解析,但 url 的其余部分未解析,还有其他建议吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite memento


    【解决方案1】:

    您的规则有这个模式^/(.*),如果规则在 htaccess 文件中,它将永远不会匹配。在 htaccess 文件中应用规则时,Mod_rewrite 会从 URI 中去除前导斜杠。删除模式中的前导斜杠:

    RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}/$1]
    

    等等……

    【讨论】:

    • 谢谢,新链接现在是 purl.org/memento/timegate/http://(null)>;rel=timegate,应该是 purl.org/memento/timegate/http://localhost/…,您能提供更多建议吗?
    【解决方案2】:

    为 Memento 配置 Apache 服务器

    1.使用 .htaccess 的 HTTP 标头

    RewriteEngine On
    
    RewriteRule ^(.*) - [E=ORIGPROTO:http]
    RewriteCond %{HTTPS} on
    RewriteRule ^(.*) - [E=ORIGPROTO:https]
    RewriteRule ^(.*) - [E=ORIGURI:%{HTTP_HOST}]
    RewriteCond %{THE_REQUEST} \s/+([^\s?]+)
    RewriteRule ^ - [E=ORIGQRY:%1]
    
    Header always set Link "<http://purl.org/memento/timegate/%{ORIGPROTO}e://%{ORIGURI}e%{ORIGQRY}e>;rel=timegate"
    

    见:htaccess - how to capture the current rewrited url?

    2。使用 PHP 的 HTTP 标头

       function get_canonical_url($proto='http://'){
            $canonical_url = $proto;
            if($_SERVER["SERVER_PORT"] != "80") {
                    $canonical_url .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] .  $_SERVER["REQUEST_URI"];
            } 
            else {
                    $canonical_url.=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
            }
            return $canonical_url;
        }
    
        header('Link: <'.get_canonical_url().'>; rel="canonical"');
    

    资源:http://moz.com/blog/how-to-advanced-relcanonical-http-headers

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 1970-01-01
      • 2015-04-10
      • 2012-09-13
      • 2011-07-09
      • 2011-01-01
      • 2015-07-29
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多