How to redirect to a HTML page AND keep the same URL?

How are you doing?

I will try to make this as simple as possible:

I have a HTML called users.html. That HTML contains a JS script that fetches a user’s information on the server. It gets the username using the URL. For example:

webpage.com/users/kibebr or webpage.com/users.html/kibebr

the JS script reads the kibebr and fetches the data.

It’s working great on my computer but how can I use Netlify redirects to achieve the same?

I have tried:

[[redirects]]
  from = "/users/*"
  to = "/users"
  status = 200
  force = false
  headers = {X-From = "Netlify"}

When I go to webpage.com/users the server rejects it, works fine!

However, when I add a slash like webpage.com/users/kibebr, I get

Uncaught SyntaxError: Unexpected token '<'

NOTE: I do have a web app running with the issue, you can see it with:
https://pixelsketch.netlify.app/users
https://pixelsketch.netlify.app/users/username

@kibebr It’s a little unclear what you’re trying to do. Your redirect tells Netlify that anyone who tries to visit /users/kibebr should instead be sent to /users. If you want the user name to be used in a new URL somehow, then you must have something like to = "/users.html/:splat".

@gregraven

I am so sorry for not explaining it concisely.

Basically, my users.html has a script that reads the window URL and gets the last string after a slash.

For example: webpage.com/users/theusername

The script will read the theusername. Then, it will fetch theusename's information from the server using AJAX and plug the data in the DOM.

I have tried your suggestion but it seems like Netlify understands that theusername is another page and returns 404.

Anyway, thank you so much for your help.

@kibebr OK, so you’re not actually redirecting to another page? I think you’re going to have to stick with your JavaScript solution because redirects seem not to be designed for this task.

@gregraven

Yep, seems like redirects aren’t actually necessary. I have tried removing all redirects and the “issue” is that when I type in webpage.com/users/theusername, Netlify reads theusername as another page, and tries to get theusername.html.

Basically I’m trying to make theusername a query, just like
webpage.com/users?name=theusername (this works fine with the necessary modifications)
but I’m not sure if Netlify supports it using only slashes:
webpage.com/users/theusername

And thank you once again for taking your time to look into this

Hey @kibebr,

I think the cleanest solution here would be query parameters which would result in a URL such as webpage.com/users?theusername. You won’t need to do this but, if you need to redirect them in future, we support query parameter redirects.