1

Is there a way to redirect masked or unmasked external links through a subdomain? (The No External links plugin is masking the links; masked and unmasked links are not displaying in the same time on page)

See the example below, I want to redirect If links are masked (plugin enabled):

example.com/go/externalexample.com/newstitle/ (links showing internal with MozBar)

to

rd.example.com/go/externalexample.com/newstitle/ (links showing external with MozBar)

If links are not masked (plugin disabled):

externalexample.com/newstitle/ (external link on homepage, links showing external with MozBar)

to

rd.externalexample.com/newstitle/ (links showing external with MozBar)

Thanks

merxie
  • 95
  • 10
  • Where does the 'masked' part of the question come in to play? – DriveItLikeYouStoleIt Jan 23 '19 at 18:08
  • example.com/go/externalexample.com it is a masked link. I suppose the question is for every external link masked or not. I have updated the question. – merxie Jan 23 '19 at 18:55
  • You wrote _**If links are not masked:**_ and the example is _"externalexample.com"_, but that link is for a different domain – Alon Eitan Jan 23 '19 at 19:07
  • It is an external link placed on my example.com site (news aggregator) – merxie Jan 23 '19 at 19:08
  • 1
    Why is it rd/externalexample.com and not rd.externalexample.com? – kojow7 Jan 23 '19 at 19:09
  • @kojow7 thanks for pointing that out. I have corrected it! – merxie Jan 23 '19 at 19:10
  • A couple other questions: How do you determine if a link is masked or not? Also, what do you mean that links shows internal vs links show external vs external link on homepage vs external? Are these four completely different things, if so can you describe what you are meaning by them. – kojow7 Jan 23 '19 at 19:23
  • A plugin is masking the links. Only 1 type is displayed at once on the page and yes all 4 are different types in the way of how they display on the browser status bar when you hover on them. I have updated the question. – merxie Jan 23 '19 at 19:43
  • This already has an answer [here](https://stackoverflow.com/questions/9363760/301-or-302-redirection-with-php): –  Jan 23 '19 at 20:08
  • @Chipster That is a different thing. – merxie Jan 23 '19 at 20:14

2 Answers2

1

I don't think you can 'catch' someone leaving the site (and change it then), (AFAIK).

But with javascript (Jquery) you can change all links on the site

You could change all links <a> with href attribute to something like:

$('a').each(function(){
    var url = $(this).attr('href');
    var url = url.split("://").join('://rd.');
    $(this).attr('href', url);
});

This code will split the url on :// and put ://rd. in it's place (The reason I chose :// is because you probably won't have that anywhere else in the url and it also works with non http links)

Kerwin Sneijders
  • 750
  • 13
  • 33
  • How should I test this? – merxie Jan 23 '19 at 19:50
  • 1
    The code I wrote above changes every link to: "rd.old_link". Because you asked about masked links but the result of non-masked and masked was the same. If your site uses Jquery you can past it in ` – Kerwin Sneijders Jan 23 '19 at 21:34
  • I have added this script in the footer and it is adding the rd subdomain before the https:// Is it possible to add it as https://rd.example.com ? – merxie Jan 24 '19 at 08:41
  • It gives a 404 if I click the links. The code is adding 2 instances of rd as below: rd.example.com/go/rd.externalexample.com/newstitle/ Also these links are showing internal, not external as I would want them. – merxie Jan 24 '19 at 09:56
  • In your example it didn't have any https:// in the second url. Can you show an example you actually use. That would help. – Kerwin Sneijders Jan 24 '19 at 19:21
  • I don't have a test page yet, I just check it quick on main site than revert. The link shows exactly as this: https://rd.example.com/go/https://rd.www.externalexample.com/newstitle/ The second rd is not needed. – merxie Jan 24 '19 at 20:36
0

Sorry. I was in a hurry and should have been more specific.

The only way I'm aware to redirect urls in PHP is via the header function mentioned in this post.

This would allow you to set up a page "foo.php" that could be set up as a dummy redirection page:

<?php
$url = $_GET["url"];

// Do stuff...

// Later...
header("Location: http:://$url", true, 301);

then make the HREF actually point to foo?url=my_URL.

However, if you're not hard set on using PHP, you can try looking into .htaccess and mod_rewrite, which is probably more powerful and can do what it sounds like you actually want.

  • Correct me if I am wrong, but I think mod_rewrite only works for your own server, it won't work for links you click on to an external server. – kojow7 Jan 24 '19 at 04:40
  • 1
    You are not wrong. However, you can rewrite links to your server as a link to an external server, as far as I know. I've never tried it. I think that's what the OP was asking for. –  Jan 24 '19 at 04:52