Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- // Settings
- const baseUrl = 'https://domain.com/'; // Your website root
- const sitemapURL = 'https://domain.com/sitemap.xml'; // Your sitemap URL
- const notFoundTitleKeyword = 'Not Found'; // A keyword that is present in your "Not Found" page title. This might be "404", "Uh oh", "We can't find.." etc.
- const ignoreWords = ['blog', 'feed', 'guide']; // Common words to ignore. These should be low-value words that are present in many of your URLs.
- const minWordLength = 3; // Set the minimum word length in characters. This ignores low-value words like "to", "a", "the", etc.
- // Only edit below here if you know what you're doing:
- document.addEventListener('DOMContentLoaded', function() {
- if (document.title.includes(notFoundTitleKeyword)) {
- async function fetchSitemap(url) {
- try {
- const response = await fetch(url);
- const text = await response.text();
- const parser = new DOMParser();
- const xmlDoc = parser.parseFromString(text, "text/xml");
- const urls = xmlDoc.querySelectorAll('loc');
- return Array.from(urls).map(node => node.textContent);
- } catch (error) {
- console.error('Error fetching sitemap:', error);
- return [];
- }
- }
- function processCurrentURL() {
- const path = window.location.pathname;
- let segments = path.split('/').filter(segment => segment.length > 0 && !ignoreWords.includes(segment));
- segments = segments.map(segment => {
- if (segment.match(/^\d+$/)) return null;
- return segment.replace(/-/g, ' ');
- }).filter(segment => segment != null && segment.length > minWordLength);
- return segments.join(' ').split(' ').filter(word => word.length > minWordLength);
- }
- function findClosestMatch(currentKeywords, urls) {
- let bestMatch = '';
- let bestScore = 0;
- urls.forEach(url => {
- const urlParts = url.replace('https://colonyroofers.com/', '').split('/').filter(part => part.length);
- let score = 0;
- currentKeywords.forEach(keyword => {
- if(urlParts.some(part => part.includes(keyword))) score++;
- });
- if(score > bestScore) {
- bestScore = score;
- bestMatch = url;
- }
- });
- return bestMatch;
- }
- async function redirectToClosestMatch() {
- const sitemapUrls = await fetchSitemap(sitemapURL);
- if (sitemapUrls.length === 0) {
- console.log('Sitemap is empty or could not be fetched.');
- return;
- }
- const currentKeywords = processCurrentURL();
- const closestMatch = findClosestMatch(currentKeywords, sitemapUrls);
- if(closestMatch) {
- console.log('Redirecting to:', closestMatch);
- window.location.replace(closestMatch);
- } else {
- console.log('No close match found.');
- }
- }
- redirectToClosestMatch();
- }
- else {
- console.log('Incorrect page title:', document.title);
- }
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement