如何使用 .htaccess 将任何外部链接重定向到特定的 url 格式? [英] How to redirect ANY external links to a specific url format using .htaccess?

查看:38
本文介绍了如何使用 .htaccess 将任何外部链接重定向到特定的 url 格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重定向所有外部链接,即我网站中的 http://www.someothersite.com/anything 之类的链接(比如 http://www.example.com) 到 http://www.example.com/something.

I want to redirect all the external links, i.e., links like http://www.someothersite.com/anything in my site(say http://www.example.com) to http://www.example.com/something.

这里我面临的问题是如何处理 http://www.someothersite.com/anything 因为它的值不是固定的.它可以来自任何不同的域.

Here the problem I am facing is how to take care of http://www.someothersite.com/anything because its value is not fixed. It could be from any different domain.

推荐答案

首先,.htaccess 配置一个 HTTP 服务器,它处理传入请求,对资源的请求您的服务器,而不是外部服务器.但是由于您明确要求使用 .htaccess 重定向外部链接的方式,我将概述三种方法:

First of all, .htaccess configures an HTTP Server which handles incoming requests, requests for resources on your server, not external ones. But since you've explicitely asked for a .htaccess way of redirecting external links, I will outline three approaches:

  1. 使用 .htaccessmod_substitute 将页面上的所有外部链接替换为固定的内部链接.
  2. 使用 PHP 和输出缓冲将所有外部链接替换为内部链接.
  3. 使用 JavaScript 在点击时将外部链接重定向到内部链接.
  1. Use .htaccess and mod_substitute to replace all external links on your pages with a fixed internal one.
  2. Use PHP and output buffering to replace all external links with internal ones.
  3. Use JavaScript to redirect external links to internal ones on click.

所有方法都有优点和缺点.

All methods have advantages and disadvantages.

如果没有测试过这个,但理论上应该可以.

If have not tested this, but theoretically, it should work.

将以下内容放入您的 .htaccess 并将 www.yourdomain.com 替换为您的域:

Place the following in your .htaccess and replace www.yourdomain.com with your domain:

AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|(<a\s[^>]*)href=\"https?://([^/]+)(?<!www\.yourdomain\.com)/[^\"]*\"|$1href=\"http://www.yourdomain.com/your-fixed-link\"|i"

这会向所有包含 HTML 内容的请求正文添加一个输出过滤器,并使用正则表达式将每个链接的 href 属性中的任何外部 URL 替换为您的固定链接.

This adds an output filter to all request bodies with HTML content and uses a regular expression to replace any external URL in the href attribute of every link with your fixed one.

缺点:

  • 您的域必须是固定的(sub2.yourdomain.comyourdomain.com 将被视为外部域).
  • 您必须使用已加载 mod_substitute 的 Apache HTTP 服务器.
  • Your domain must be fixed (sub2.yourdomain.com and yourdomain.com will be considered external).
  • You must use an Apache HTTP server which has mod_substitute loaded.

如果您已经使用 PHP 生成页面,则可以使用回调添加输出缓冲,该回调将外部链接替换为内部链接:

If you already use PHP to generate your pages, you can add output buffering with a callback which replaces external links with internal ones:

<?php
function replace_links($html)
{
    return preg_replace('~(<a\s[^>]*)href="https?://([^/]+)(?<!www\\.yourdomain\\.com)/[^"]*"~i', '$1href="http://www.yourdomain.com/your-fixed-link"', $html);
}

ob_start('replace_links');

// generate page
?>

这将阻止 PHP 将页面内容直接发送到浏览器,而是将所有内容写入内部缓冲区.在脚本结束时,回调被执行,并且可能会在缓冲区内容发送到浏览器之前修改该缓冲区的内容.

This will prevent PHP from sending the content of the page directly to the browser, but writes everything to an internal buffer. At the end of the script, the callback is executed and may modify the content of that buffer before it will be sent to the browser.

相对于 .htaccess 方法的优势: 对替换的更多控制.您可以使用 preg_replace_callback 结合检查链接中实际域的回调从替换中排除多个内部域.此外,替换后的 URL 不必固定.

Advantages over the .htaccess method: More control over the substitution. You could exclude more than one internal domain from the replacement by using preg_replace_callback in combination with a callback which checks the actual domain in the link. Furthermore, the substituted URL does not have to be fixed.

以下 JavaScript 可能是最流行的实现您想要的方式:

The following JavaScript probably is the most popular way of doing what you want:

(function() {
    function redirectLink(evt) {
        var matches = /^https?:\/\/([^\/]+)\//i.exec(this.href);
        if (matches && matches[1] != 'www.yourdomain.com') {
            window.location.href = 'http://www.yourdomain.com/your-fixed-link';
            evt.preventDefault();
            return false;
        }
    }

    var links = document.getElementsByTagName('a'), i;
    for (i = 0; i < links.length; i++)
        links[i].addEventListener('click', redirectLink, false);
})();

只需确保此代码位于所有链接之后(例如在文档末尾)或在 DOMContentLoaded 事件上执行.

Just make sure that this code is placed after all links (e.g. at the end of the document) or is executed on the DOMContentLoaded event.

.htaccess 方法相比的优势:与上述 PHP 方法相同.

Advantages over the .htaccess method: Same as with the PHP method described above.

缺点:用户必须启用 JavaScript,因此不能保证始终有效.

Disadvantages: The user must have JavaScript enabled, so this is not guaranteed to work always.

这篇关于如何使用 .htaccess 将任何外部链接重定向到特定的 url 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆