Join our telegram Contact Us Join Now!
Posts

JavaScript Function to Generate and Redirect to a Clean URL Slug (redirect wordpress - blogspot)

 <script>

  (function() {

    // Function to generate the slug from the title

    function generateSlug(title) {

      // Convert title to lowercase and remove special characters

      var words = title.toLowerCase()

        .replace(/[^\w\s]/g, '') // Remove punctuation

        .split(/\s+/)             // Split into words

        .filter(word => word.length > 0); // Remove empty strings


      // Create slug with up to 6 words

      var slug = words.slice(0, 6).join('-');


      // Ensure the slug is less than 40 characters and trim any trailing hyphen

      return slug.length > 40 ? slug.substring(0, 40).replace(/-+$/, '') : slug.replace(/-+$/, '');

    }


    // Function to get the current year and month

    function getCurrentYearMonth() {

      var now = new Date();

      var year = now.getFullYear();

      var month = ('0' + (now.getMonth() + 1)).slice(-2); // Format month as two digits

      return `${year}/${month}`;

    }


    // Function to generate the redirected URL

    function generateRedirectUrl(title) {

      var baseUrl = `https://bharatnews.vebnox.com/${getCurrentYearMonth()}/`;

      var slug = generateSlug(title);

      var newUrl = baseUrl + slug + '.html'; // Append .html to the slug

      return newUrl;

    }


    // Example title - replace this with dynamic title extraction if needed

    var title = document.title || "Default Article Title";


    // Generate redirect URL and redirect

    var redirectUrl = generateRedirectUrl(title);

    window.location.href = redirectUrl;

  })();

</script>


Rate This Article

Thanks for reading: JavaScript Function to Generate and Redirect to a Clean URL Slug (redirect wordpress - blogspot), Sorry, my English is bad:)

Getting Info...

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.