混淆 WordPress 中的链接(出于 SEO 目的)

发布于 2025-01-18 11:26:18 字数 483 浏览 3 评论 0 原文

链接混淆是一个越来越常见的话题,目的是通过屏蔽不重要的链接来为其他人提供更多的权重来改善搜索引擎优化。

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

它必须将 元素转换为 等其他元素,并且不再可见 href 属性或任何实际 URL ,这样机器人就看不到任何链接。

它必须在渲染源代码之前完成,而不是在 JavaScript 中覆盖。

示例:

Hello

变为:

Hello< /span>

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

Link obfuscation is a more and more common topic in order to improve SEO by masking the non-important links to provide more weight to others.

I'm looking for an efficient way to obfuscate links in Wordpress, directly in the source code obviously, for example by adding a special class to my links.

It must turn the <a> element into something else like a <span>, with no more visible href attribute nor any actual URL, so that robots cannot see any link.

It must be done before rendering the source code, not overridden in JavaScript.

Example :

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

turns into :

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

then some JS allows click on the element to redirect to the original link.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑕疵 2025-01-25 11:26:18

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

首先,您可以在标签之间的组后面添加一个问号,因为根据 W3C 验证器,没有文本的链接是有效的,即 .

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

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

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

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

'#]+((?<=\s)href=(\"|\')([^\"\']*)(\'|\")[ ^>]+(?<=\ s)class=(\"|\')[^\'\"]*(?]+(?& lt;=\s)href=(\"|\')([^\"\']*)(\'|\"))[^>]*>(.*)<\/a> ;#miUs'

您可能有兴趣检查您的正则表达式我的(检查单元测试)。

I suggest to modify the "detection" regular expression used with preg_replace_callback.

First of all, you may add a question mark right after the group between the tags as a link having no text is valid according to W3C validator i.e. <a href=...></a>.

A second suggestion is to add (?<!\w|-) before and (?!\w|-) after the class name to detect. Otherwise, you get false detections with class names like do-not-obfuscate_this or notobfuscated.

My third suggestion is to add (?<=\s) before each href and class word. To avoid matching custom attributes like data-href= or unclassify=.

My last suggestion is to remove (?!<a) from the end as the expression is non greedy (and nesting <a> tags -the idea between this?- is not allowed). Thus, (.+(?!<a))<\/a> should become (.+)<\/a>. And this, as it should be combined to the suggestion one, should lead us to (.*)<\/a> (no need for (.+)?<\/a>).

Finaly, the regular expression I use is:

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

You may be interested in checking the differences between your regexp and mine (check the unit tests).

时光沙漏 2025-01-25 11:26:18

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

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

另外,请务必编辑上面的样式,或者删除它们并在您自己的 CSS 文件中设置“akn-obf-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

// Edit 2023-02-08 - improved class regex thanks to @MadMaxInfinity on Stack Overflow again.

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[^>]+(?<=\s)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

考虑检查链接以防万一我更新了代码并忘记在这里更新

I ended up making my own system that allows me to obfuscate any link easily.

Add the following code to your child theme's functions.php file, then just add the class "obfuscate" to any element to obfuscate its link by replacing it with a element with no readable link.

Also be sure to edit styles above, or delet them and style the "akn-obf-link" class in your own CSS file, so that it looks like a link to the visitor.

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

// Edit 2023-02-08 - improved class regex thanks to @MadMaxInfinity on Stack Overflow again.

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[^>]+(?<=\s)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
}

I'm also sharing the code on this Pastebin: https://pastebin.com/cXEBSVFn

Consider checking the link just in case I updated the code on it and forgot to update it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文