Rip Hunter
Rip Hunter

Reputation: 97

How to redirect ANY external links to a specific url format using .htaccess?

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.

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.

Upvotes: 3

Views: 4402

Answers (1)

Callidior
Callidior

Reputation: 2904

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. 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.

1. .htaccess and mod_substitute

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

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"

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.

Drawbacks:

  • 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.

2. PHP and output buffering

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
?>

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.

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.

3. Redirecting with 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);
})();

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.

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

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

Upvotes: 3

Related Questions