Skip to main content
Solved

How to "redirect" users from a given URL to a different page every time


Daniele Cmty
Forum|alt.badge.img+1

Dear Community,
I’ve got a question

In practical terms: How do  I “redirect” any Ideation Link to send to another page instead?


In technical terms:  How do I "redirect" users that visit a  URL that contains a specific string (community.mycompany.com/ideas), to a different page we have defined (“new URL”)?

 

I know of a Third-Party-Script that was shared with me by the amazing Lauro from Support (shoutout)!

 

This one works fine ✅  But it only applies to URLs that exactly match “old URL”.

<script>
if (window.location.href == 'https://community.mycompany.com/ideas') {
  window.location = "new URL"
}
</script>

Now I don’t want only URLs that exactly match https://community.mycompany.com/ideas

 

The one below did not work well ❌

<script> 

if (!window.location.pathname.startsWith('/community.mycompany.com/ideas')) { 
window.location.href = "new URL"; } 

</script>

This is the one I actually want to use, so for all pages that contain the string community.mycompany.com/ideas, so any kind of ideation content, including the homepage.


It did not work well, meaning: any destination page kept reloading.

 

Does anybody know what I might be doing wrong?

 

Or is there an alternative, better solution?

Thank you so much in advance,

Daniele

 

Best answer by TomW

Hi @Daniele Cmty, interesting question! I have some scripts I only run on certain pages and categories. I use this piece of code:

if (window.location.pathname.startsWith('/category-12')) {
        // do a thing
}

You were close with your code, you used the domain part of the url but you should only use the pathname. To make it work, this should be the way to go:

if (window.location.pathname.startsWith('/ideas')) { 
    window.location.href = "https://example.com/thing";
}

 

View original
Did you find this topic helpful?

2 replies

TomW
  • Helper ⭐️⭐️
  • 29 replies
  • Answer
  • September 2, 2024

Hi @Daniele Cmty, interesting question! I have some scripts I only run on certain pages and categories. I use this piece of code:

if (window.location.pathname.startsWith('/category-12')) {
        // do a thing
}

You were close with your code, you used the domain part of the url but you should only use the pathname. To make it work, this should be the way to go:

if (window.location.pathname.startsWith('/ideas')) { 
    window.location.href = "https://example.com/thing";
}

 


Daniele Cmty
Forum|alt.badge.img+1
  • Author
  • Helper ⭐️⭐️
  • 276 replies
  • September 9, 2024

Thank you so much, @TomW !!! 🎉😍


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings