Redirect URL based on subdomain - istio

I need to do redirection based on the subdomain using ambassador, isito, kuma or kong
eg
test.example.com/ should be redirected to test.example.com/realms/test/sso
test1.example.com/ should be redirected to test1.example.com/realms/test1/sso
Im able to do it via nginx
server_name (*.).example.com
location = / {
return https://$1.example.com/realm/$1/sso;
}

Related

Redirect old domain to new domain, including /en/

Little stuck but I'm trying to redirect an old domain name to a new domain, which is working to a point. However, we have a long list of URLs from our old website (using the old domain). Which have /en/ appended at the end.
So the issue is when I link olddomain.com/en/old-url to newdomain.com/new-url it throws a 404 as it's not picking up the '/en/'. I've compiled a long list of 301 redirects inside the Django admin, but they don't include the '/en/'. Which is where the issue is. Ideally, I want to add something to my nginx config that tells the domain to redirect even if the /en/ is included.
So Far I have something like this:
server {
listen 80;
server_name olddomain.co.uk www.olddomain.co.uk olddomain.co.uk/en/
return 301 https://www.newdomain.co.uk$request_uri;
}
server {
HTTPS
server_name olddomain.co.uk www.olddomain.co.uk olddomain.co.uk/en/;
#return 301 https://www.newdomain.co.uk$request_uri;
listen 443;
}
Thanks in advance.

How to redirect static files requests to https in Nginx?

I have two versions of site with urls: http://example.com and https://example.com.
I want to redirect all requests to static content (files that is ended with .html, .htm, .js) to https version of my site.
So, I created the rule:
location ~ "\.(htm|html|js|css|svg|png)$" {
return 307 https://example.com$request_uri;
}
With this rule browser changes address of my site to https://example.com.
But I don't want to change address, I want that all requests to static files but not to index.html (main html of my site) will be redirected to https version.
How can I add something like AND NOT index.html to regex ~ "\.(htm|html|js|css|svg|png)$"?
Try:
root /path/to/root;
location = /index.html {
}
location ~ "\.(htm|html|js|css|svg|png)$" {
return 307 https://example.com$request_uri;
}
The location = block has highest precedence (the order is not important).
Because of an explicit or implicit index index.html statement, the URI / causes nginx to look for /index.html. The empty location block will cause the static file to be served, and the return 307 avoided.
See this document for more.

nginx default redirects and custom redirects

OS debian 8;
im trying to write nginx config what will
1) redirect all requests from root domain.a to domain.b
2) redirect all requests with route from domain.a/$1 to domain domain.a/api/route/$1
I was able to acomplish 2) but when i type domain.a in browser, it shows nginx default page. I expected to forward it to domain.b
server {
listen 80;
server_name domain.a;
#should redirect all other requests to domain.b , but it not happens
return 301 domain.b;
# correctly redirects from domain.a to domain.b api
location ~/(.*)$ {
return 301 https://domain.b/api/route/$1;
}
}
You can isolate the / URI by using the location = / syntax. This may work for you:
location = / {
return 301 https://domain.b/;
}
location / {
return 301 https://domain.b/api/route$uri;
}
See this document for details.

Can NGINX change the response code after a proxy_pass?

So I have an internal API server namespaced under /api/, and I want to pass all other requests to an Amazon S3 static site using proxy_pass. This all works fine, it's just since Amazon is serving a single page app, I want to always return the same HTML file. They way I did this with the S3 server, was to set the index and error page as the same file. It all looks fine on the surface, but for all other requests besides /, the S3 instance returns a 404. Can I use NGINX to change this to a 200 before returning it to the client?
server {
listen 80;
server_name example.com;
location /api/ {
# serve internal app
}
location / {
proxy_pass http://example.amazonaws.com/;
# ALWAYS RETURN A 200
}
}
You should be able to use the error_page and proxy_intercept_errors directives to achieve this. Something like this should do the trick.
location / {
proxy_pass http://example.amazonaws.com/;
proxy_intercept_errors on;
error_page 404 =302 /your_html_file
}
error_page
proxy_intercept_errors
You can internally rewrite all URLs to the document you want served. This avoids the error handling cycle and problematic redirects.
It would be something like (untested):
location / {
proxy_pass http://example.amazonaws.com/;
rewrite ^.* /index.html
}
Note that you will want to only use full or root-relative URLs in your doc, because you don't know if the docs is served from a subdirectory.
You'd also be wise to have JS code validate the URL and optionally redirect to one you consider valid. Otherwise 3rd party sites could link to offensive URLs and get them in search indexes!

nginx rewrite url only with certain subdomain

I am trying to make my nginx rewriting all the urls with certain subdomain (m.example.com) to add string: ?theme=XXX to the end of the file
In my nignx server configuration i have:
server{
server_name example.com m.example.com y.example.com
(...)
}
Now, I want to rewrite all http requests going ONLY through m.example.com to add to the end of the url ?theme=XXX
I have the following rewrite:
rewrite ^(.*)$ $1?theme=XXX? break;
but i don't know how to make rewriting only requests from m.example.com (not from all server names)