Score:0

rewrite rule nginx for everything in a domain

cn flag

I'd like to make a rewrite of my domain name.domain.tld to another URL. I managed to do this but if I type name.domain.tld/dashboard or anything after the tld I still get the content like without the rule.

In the config I have rewrite ^/$ https://new.url redirect;

So how can I do a redirect no matter what is being typed in after name.domain.tld?

Ivan Shatsky avatar
gr flag
Do you want to preserve an existing request URI (making `name.domain.tld/dashboard` redirected to `new.domain.tld/dashboard`) or redirect every request to the `new.domain.tld/`?
Iron Dude avatar
cn flag
No. The original URL is something totally different. Like `old.url` or `old.url/dashboard` but I want people to be redirected to `new.url` if they type in `old.url/$` so no matter what subdir of `old.url` they use.
Ivan Shatsky avatar
gr flag
What is that `new.url` in your rewrite rule? Where is the domain name in it? What do you mean by `old.url/$`? Do you mean you want to rewrite URIs ending with a dollar sign? Please, make your question more clarified.
Iron Dude avatar
cn flag
The $ is just a wildcard. So I want URL A (the old one) to be redireted to URL B no matter what subdirectory the person using URL A is using. With what I have the redirect only works when no subdirs are being used.
Ivan Shatsky avatar
gr flag
So you want to redirect an URL within the same domain? You do not change the domain with redirection?
Iron Dude avatar
cn flag
No. As I said. The old URL ist something like `old.url` and may have some subdirs and I want ALL requests that are being made to the old url no matter if a subdir is given or not redirected to a total different URL that has nothing to do whatsoever with the old URL.
Ivan Shatsky avatar
gr flag
Ok, it is that `https://new.url` that confused me. You can use a redirect address in a form `<scheme>://domain/url` or simply `url` but not the `<scheme>://url`. You are using `^` start-of-the-string anchor and `$` end-of-the-string anchor making your `^/$` regex matching only the root `/` request and nothing else. Use `rewrite ^ /new/url;` to match any request (any string has a beginning, yeah?)
Iron Dude avatar
cn flag
That did it. Thank you!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.