how to remove /?fbclid=… in nuxt url

半世苍凉 提交于 2020-03-05 00:23:55

问题


hello there i like to remove the facebook analytic forced url parameter /?fbclid= https://www.example.com/?fbclid=..., from my host url, when redirected from facebook by clicking the url, the problem is the nuxt-link-exact-active class is not applied if redirected with this parameter. Thanks


回答1:


In simple cases like https://www.example.com/?fbclid=... where fbclid is the only parameter, it should be a trivial Javascript like that:

 <script>
  // ideally this is on top of page; works on bottom as well

  if(/^\?fbclid=/.test(location.search))
     location.replace(location.href.replace(/\?fbclid.+/, ""));

 </script>

This checks if ?fbclid=... is a URL search parameter and navigates to the same location with that part removed.


It may also be fine to remove any search parameter and not checking for fbclid.
 <script>
   if(location.search) location.replace(location.href.replace(/\?.+/, ""));
 </script>



回答2:


i could finally solved it with this:

methods: {
  removeFacebookHook() {
    var fbParam = 'fbclid';

    // Check if param exists
    if (location.search.indexOf(fbParam + '=') !== -1) {
      var replace = '';

      try {
        var url = new URL(location);
        url.searchParams.delete(fbParam);
        replace = url.href;

        // Check if locale exists
        if (window.location.href.indexOf(this.locale) > -1) {
          window.history.replaceState(null, null, "/" + this.locale);
        };

      } catch (ex) {
        var regExp = new RegExp('[?&]' + fbParam + '=.*$');
        replace = location.search.replace(regExp, '');
        replace = location.pathname + replace + location.hash;
      }

      history.replaceState(null, '', replace);
    }
  }
}

with the help of this post modiyf urls I keep the nuxt-i18n route locale working with href.indexOf ! Unfortunately the nuxt alwaysRedirect made me remove the switcher...




回答3:


For simple cases like https://www.example.com/?fbclid=... where fbclid is the first and only parameter, it can be done by a simple server configuration.

So for example put this in the .htaccess file:

RewriteEngine on
<if "%{QUERY_STRING} =~ /^fbclid=/">
 RewriteRule  .  %{REQUEST_URI}?   [R=301,L]
</if>

Note the ? after %{REQUEST_URI}. It deletes the query string completely.


In other cases (where fbclid was appended to other parameters) this example does nothing - more complicated code is needed for that.

来源:https://stackoverflow.com/questions/60208823/how-to-remove-fbclid-in-nuxt-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!