Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+7 votes
2.3k views
in Q2A Core by

I tried the following without succes, anybody know what is wrong in this code:

This is what I have now:
function qa_html_convert_urls($html, $newwindow=false)
/*
    Return $html with any URLs converted into links (with nofollow and in a new window if $newwindow)
    URL regular expressions can get crazy: http://internet.ls-la.net/folklore/url-regexpr.html
    So this is something quick and dirty that should do the trick in most cases
*/
    {
        return trim(preg_replace('/([^A-Za-z0-9])((http|https|ftp):\/\/([^\s&<>"\'\.])+\.([^\s&<>"\']|&amp;)+)/i', '\1<A HREF="\2" rel="nofollow"'.($newwindow ? ' target="_blank"' : '').'>\2</A>', ' '.$html.' '));
     }
 // remove nofollow for my links
    $search = '/href="http:\/\/YOURDOMAIN\/([^"]*)" rel="nofollow"/i';
    $replace = 'href="http://YOURDOMAIN/\1"';       
    $html = trim( preg_replace($search, $replace, $html) );
       
    return $html;

This apparently does not work.

 

2 Answers

+4 votes
by
edited by
 
Best answer

I actually do this within my theme now, which means it will survive upgrades. I'll post instructions here.

1. Create a custom theme if you don't already have one.

2. In qa-theme.php, add this function in the qa_html_theme class, replacing EXAMPLE.COM with your domain:

function _remove_nofollow( $str )

{
    $search1 = '#<a rel="nofollow" href="http://EXAMPLE.COM("|/[^"]*")#i';
    $search2 = '#<a href="http://EXAMPLE.COM("|/[^"]*") rel="nofollow"#i';
    $replace = '<a href="http://EXAMPLE.COM\1';
    $str = preg_replace( $search1, $replace, $str );
    $str = preg_replace( $search2, $replace, $str );
    return $str;
}

3. Override the q_view_contenta_item_content and c_item_content functions to call this function:

function q_view_content($q_view)
{
    if ( !empty($q_view['content']) )
        $q_view['content'] = $this->_remove_nofollow( $q_view['content'] );
    parent::q_view_content($q_view);
}
function a_item_content($a_item)
{
    if ( !empty($a_item['content']) )
        $a_item['content'] = $this->_remove_nofollow( $a_item['content'] );
    parent::a_item_content($a_item);
}
function c_item_content($c_item)
{
    if ( !empty($c_item['content']) )
        $c_item['content'] = $this->_remove_nofollow( $c_item['content'] );
    parent::c_item_content($c_item);
}

Hope this helps!

UPDATED: fixes for comments where nofollow gets added at the end for some reason, and slightly cleaner code.

by
Thanks!!!

That works like a charm, thanks again.
by
I am looking into doing the exact same thing for my rss feed.

In the title of the rss link to any page it already is ok, but whenever there is a link in the description it is now still turned into a nofollow link.

How can I get the rss feed to do the same and get rid of the nofollow for links to my own site?
by
Looks like the RSS code is generated somewhere else in qa-feed.php. TBH it doesn't matter whether the RSS feed has nofollow or not since search engines don't "rank" feeds.
by
my RSS feeds are posted to rss aggregators and therefore they lead to backlinks, and then an extra link in the description without nofollow is better IMO, so would you be able to help me out?
I looked at qa-feed.php but can not find where it does the nofollow thing.

thanks
asked Dec 20, 2011 in Q2A Core by anonymous Does this entire code go into qa-theme.php?
by
Thanks for this hack, it certainly works...but i found it is not removing the urls for files or attachments. Is it ok or i have write extra piece of code for the attachments
by
@Kai, I'm not sure why this causes the indentation problem, as all output is sent through the output function as it should be.
by
I tested it, with the hack I got the "html message" without the hack, no message. Strange to me as well, maybe this has to do with unicode stuff? I have no idea...
–1 vote
by

you can use Q2A SEO Links plugin.

by
wrost..................
...