<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:)