Skip to content
Advertisement

How do I make the URL for my webpage work if the last part of the path is a variable?

I have written a REST api in Java and a turn based online client game that utilizes the api. When a player creates a new game instance, other players can join given the game id via a URL.

So say Player1 creates a new game instance from the page www.mysite.com and an invite URL is generated that looks like so www.mysite.com/{id}.

Instead of actually going to that invalid URL since no page is associated with it, How do I make it so that Player 2 will just go to the default page instead so that I can later grab the ID path variable and handle the joining process?

Advertisement

Answer

Two ways:

One: use a query parameter

Simply instead of /{id} use ?id={id}.

This way you can get the parameter from javascript and the webserver knows what to do.

Two: setup webserver to serve the same file by pattern

This will depend on the webserver, but basically what you are doing is saying if request url is of a specific pattern serve that file.

For apache that would look something like:

RewriteRule ^(.*) index.html [NC,L]
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement