【问题标题】:Obfuscate links in Wordpress (for SEO purpose)混淆 Wordpress 中的链接(用于 SEO 目的)
【发布时间】:2023-01-26 19:06:09
【问题描述】:

链接混淆是一个越来越普遍的话题,目的是通过屏蔽不重要的链接为其他链接提供更多权重来改进 SEO。

我正在寻找一种有效的方法来混淆 Wordpress 中的链接,显然是直接在源代码中,例如通过向我的链接添加一个特殊的类。

它必须将 <a> 元素变成类似 <span> 的其他元素,不再可见参考资料属性或任何实际 URL,以便机器人无法看到任何链接。

必须做呈现源代码,而不是在 JavaScript 中被覆盖。

例子 :

<a href="https://example.com">Hello</a>

变成 :

<span data-o="CRYPTEDLINK">Hello</span>

然后一些 JS 允许点击元素重定向到原始链接。

【问题讨论】:

    标签: php wordpress hyperlink seo obfuscation


    【解决方案1】:

    谢谢你,非常有趣。

    我建议修改与preg_replace_callback一起使用的“检测”正则表达式。

    首先,您可以在标签之间的组之后添加一个问号,因为根据 W3C 验证程序,没有文本的链接是有效的,即 <a href=...></a>

    第二个建议是在要检测的类名之前和之后添加(?<!w|-)(?!w|-)。否则,您会得到类名称为 do-not-obfuscate_thisnotobfuscated 的错误检测。

    我的第三个建议是在每个hrefclass 单词之前添加(?<=s)。避免匹配 data-href=unclassify= 等自定义属性。

    我最后的建议是从末尾删除 (?!<a),因为表达式是非贪婪的(并且嵌套 <a> 标签-这之间的想法?-不被允许)。因此,(.+(?!<a))</a> 应该变成 (.+)</a>。而这个,因为它应该与建议一结合,应该引导我们到(.*)</a>(不需要(.+)?</a>)。

    最后,我使用的正则表达式是:

    '#<a[^>]+((?<=s)href=("|')([^"']*)('|")[^>]+(?<=s)class=("|')[^'"]*(?<!w|-)obfuscate(?!w|-)[^'"]*("|')|(?<=s)class=("|')[^'"]*(?<!w|-)obfuscate(?!w|-)[^'"]*("|')[^>]+(?<=s)href=("|')([^"']*)('|"))[^>]*>(.*)</a>#miUs'

    您可能有兴趣检查 your regexpmine 之间的区别(检查单元测试)。

    希望它对你有帮助,就像你的代码对我有帮助一样!

    【讨论】:

    • 非常感谢您的贡献!你的正则表达式确实比我的好得多,我编辑了我的编码并为你的答案添加了信用。
    【解决方案2】:

    我最终制作了自己的系统,可以让我轻松混淆任何链接。

    将以下代码添加到您的子主题函数.php文件,然后只需将类“obfuscate”添加到任何元素以通过将其替换为没有可读链接的元素来混淆其链接。

    还要确保编辑上面的样式,或删除它们并在您自己的 CSS 文件中设置“akan-of-link”类的样式,以便它看起来像访问者的链接。

    <?php
    
    /**************************************************************************************
    |* Links obfuscation - add class "obfuscate" to any <a> element to obfuscate its link *|
    **************************************************************************************/
    
    // Add this code to your child theme's functions.php file, then just add the class "obfuscate" to any <a> element to obfuscate its link by replacing it with a <span> element with no readable link.
    // The obfuscated elements inherits the original <a> element's classes, along with a "akn-obf-link" class, so you might need to add CSS to style the "akn-obf-link" class so that it looks like a link to the visitor, maybe at least to add a cursor:pointer.
    // On right click, the obfuscated link will be wrapped with a proper <a> element with the "akn-deobf-link" for a brief moment, so that a proper context menu appears, you can remove that behaviour by setting the "deobfuscate_on_right_click" option to false in the code bellow.
    
    // Edit 2022-04-05 - modified regex to allow for html elements and new lines into the <a> element, modified callback so the obfuscated element inherits the original link's classes, modified JS to add mousewheel click and right click options.
    
    // Edit 2023-01-26 - greatly improved regex thanks to @MadMaxInfinity on Stack Overflow, it now both allows more matches in different scenarios and returns less false positives matches, more infos on his answer here: https://stackoverflow.com/a/75234749/2498324
    
    add_action('wp_loaded', 'buffer_start');
    function buffer_start() {
        ob_start('akn_ofbuscate_buffer');
    }
    add_action('shutdown', 'buffer_end');
    function buffer_end() {
        ob_end_flush();
    }
    function akn_ofbuscate_buffer($buffer) {
        $result = preg_replace_callback('#<a[^>]+((?<=s)href=("|')([^"']*)('|")[^>]+(?<=s)class=("|')[^'"]*(?<!w|-)obfuscate(?!w|-)[^'"]*("|')|(?<=s)class=("|')[^'"]*(?<!w|-)obfuscate(?!w|-)[^'"]*("|')[^>]+(?<=s)href=("|')([^"']*)('|"))[^>]*>(.*)</a>#miUs', function($matches) {
            preg_match('#<a[^>]+class=["|\']([^\'"]+)["|\']#imUs',$matches[0],$matches_classes);
            $classes = trim(preg_replace('/s+/',' ',str_replace('obfuscate','',$matches_classes[1])));
            return '<span class="akn-obf-link'.($classes?' '.$classes:'').'" data-o="'.base64_encode($matches[3]?:$matches[10]).'" data-b="'.((strpos(strtolower($matches[0]),'_blank')!==false)?'1':'0').'">'.$matches[12].'</span>';
        }, $buffer);
        return $result;
    }
    add_action('wp_footer', 'akn_ofbuscate_footer_js');
    function akn_ofbuscate_footer_js() {
        ?>
            <script>
                jQuery(document).ready(function($) {
                    // options you can change
                    var deobfuscate_on_right_click = true;
                    // function to open link on click
                    function akn_ofbuscate_clicked($el,force_blank) {
                        if (typeof(force_blank)=='undefined')
                            var force_blank = false;
                        var link = atob($el.data('o'));
                        var _blank = $el.data('b');
                        if (_blank || force_blank)
                            window.open(link);
                        else
                            location.href = link;
                    }
                    // trigger link opening on click
                    $(document).on('click','.akn-obf-link',function() {
                        var $el = $(this);
                        if (!$el.closest('.akn-deobf-link').length)
                            akn_ofbuscate_clicked($el);
                    });
                    // trigger link openin in new tab on mousewheel click
                    $(document).on('mousedown','.akn-obf-link',function(e) {
                        if (e.which==2) {
                            var $el = $(this);
                            if (!$el.closest('.akn-deobf-link').length) {
                                akn_ofbuscate_clicked($el,true);
                                return true;
                            }
                        }
                    });
                    // deobfuscate link on right click so the context menu is a legit menu with link options
                    $(document).on('contextmenu','.akn-obf-link',function(e) {
                        if (deobfuscate_on_right_click) {
                            var $el = $(this);
                            if (!$el.closest('.akn-deobf-link').length) {
                                e.stopPropagation();
                                var link = atob($el.data('o'));
                                var _blank = $el.data('b');
                                $el.wrap('<a class="akn-deobf-link" href="'+link+'"'+(_blank?' target="_BLANK"':'')+'></a>').parent().trigger('contextmenu');
                                setTimeout(function() {
                                    $el.unwrap();
                                },10);
                            }
                        }
                    });
                });
            </script>
        <?php
    }
    

    我也在这个 Pastebin 上分享代码:https://pastebin.com/cXEBSVFn

    考虑检查链接,以防万一我更新了上面的代码而忘了在这里更新

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      相关资源
      最近更新 更多