Home
Support Forums

Redirecting from URL without params to URL with params

Is the following possible somehow?

/hello/ /hello/?utm_source=foo 301!

We’re looking for a way to mask lengthy URL params. An ideal scenario would be navigating to mysite.com/hello/ would then be redirected/rewritten to mysite.com/hello/?utm_source=foo.

The above is causing a redirect loop because technically they’re the same path.

Hey @abohannon

Rather than redirecting with a 301 return a 200

/hello/ /hello/?utm_source=foo 200!

@coelmay unfortunately that doesn’t work. Seems like it should, but no luck: https://heuristic-clarke-9e129f.netlify.app/hello/

redirects file:

/hello /hello/?utm_source=foo 200!

If you were using straight html, it would. But I see you are using Gatsby.

The clue is in the error perhaps

You just hit a route that doesn’t exist… the sadness.

Routing is handled within Gatsby.

@coelmay here’s a pure HTML project I just spun up with the same result: Hello

(Same _redirects file)

Edit: I added another route /hey/ /goodbye/ 200 and that doesn’t seem to do anything either. I also added /first/ /seconds/ 301! which works as expected, so the _redirects file is being used.

A 301 or 302 will redirect. With the addition of a ! it will force the redirect even if the resource being redirected from exists. If the resource being redirected to does not exist, you will of course get a 404 Not Found just as I did with your /first -> /seconds rule.

As it written in the redirect docs

Redirects with this status code will change the server response without changing the URL in the browser address bar.

So you can have

/mask    https://example.com 200

so when a user visits https://<yoursite>.netlify.app/mask/ they will see whatever is returned from https://example.com.

In your original post you talked about masking a URL, which is what the 200 does.

Ok so if I’m understanding correctly,

/hello/ /goodbye/ 200!

will keep the URL the same - /hello/ - but show the contents of /goodbye/.

Makes sense, I guess. But that’s not what my original question was about.

We’re looking for a way to mask lengthy URL params. An ideal scenario would be navigating to mysite.com/hello/ would then be redirected/rewritten to mysite.com/hello/?utm_source=foo .

Url affiliate shares with audience: mysite.com/hello/
Url when accessing affiliate url: mysite.com/hello/?utm_source=foo

It seems 200! is not the solution for this, then.

Exactly.

Apologies, I seem to have mis-read/interpreted your original question with the masking/redirect. They are two different things, but I understand what you mean.

You want to have a URL shortener without using bitly et.al. I suppose. As you already noted, your rule causes a infinite loop. The only way you could get around that is to have

/hello/    /hello/index.html?utm_source=foo

as the rule will not match twice.

You could map a path (e.g. /in) that is specifically for inbound links

/in/*       /:splat?utm_source=foo

So that /in/hello -> /hello?utm_source=foo and /in/wicked -> /wicked?utm_source=foo. But you would have to manage a lot of redirects (perhaps) and every time you wanted to add a new one, would need to redeploy your site.

You might find a solution like Bitly, or others better suit your needs.