My Location URL is something like this - www.example.com/test1/#/run1
I need to redirect it to - www.example.com/test2/#/run2
But location block in NGINX doesn't work normally with #. How do I escape it and redirect?
Related
I have several url's that I would like to rewrite in NGINX
for example:
From: app.example.com/calendar
To: calendar.example.com
Or
From: app.example.com/meetings
To: meetings.example.com
I would still like to keep the app.example.com so it's not being removed from the redirect, but just create subdomains for certain URLs.
How can I do this in NGINX conf file?
All the best.
Redirect Subdomain to Folder in NGINX
Just add the following location block in your server configuration, inside server block, above the location / block.
location ^~ /calendar {
rewrite ^/calendar/?(.*)$ https://calendar.example.com/$1 permanent;
}
In the above code replace blog.example.com with your subdomain, and /calendar with your subdirectory. The above location block will listen to all requests sent to example.com/calendar. It redirects all those requests to calendar.example.com subdomain of your website. $1 is the URL stub after subfolder in requested URL. We add permanent keyword at the end of rewrite directive to cause a permanent redirect.
So it will permanently redirect subfolder to subdomain along with URL string.
I have the following URL redirect in my Nginx server block:
location /news/ { return 301 https://example.com/blog/category/news; }
I want the above location rule to work for URLs with or without the trailing slash, eg:
OK: https://example.com/news
OK: https://example.com/news/
But not in this case:
NOT OK: https://example.com/newsletter
NOT OK: https://example.com/newsmaker
How to change my location redirect rule to reflect this requirement?
Using nginx as a reverse proxy, I'd like to mimic the index directive with proxy_pass. Therefore I'd like nginx to query /index.html instead of /, /sub/index.html instead of /sub/.
What would be the best approach to do this ?
Not sure if it's relevant, but the proxied server does answer HTTP 200 on /, but I'd still like to rewrite it to /index.html.
As the / request leaks some information by listing the directory content, I'd also like to be sure that no one will be capable of accessing it (like doing something like /sub/..).
Thanksies
Just add :
rewrite (.*)/$ $1/index.html last;
rewrite (.*)/..$ $1/../index.html last;
Should works
In the nginx.conf of website.com I put this:
rewrite ^test/(.+)$ http://www.websitenew.com/test/$1 permanent;
Then when I use curl -I www.website.com/test/en I got this in the redirection header:
Location: http://www.websitenew.com//test/en
I can see that website.com had successfully rewritten to websitenew.com and the URL scheme is also correct, but why there is one more / between websitenew.com and /test?
Thanks,
rewrite ^test/(.+)$ http://www.websitenew.com/test/$1 permanent;
This rule can't work actually, and your redirect is made by something else. There's no URI in nginx that not start with /, but your rule doesn't have / at the beginning.
How can I redirect "http://domain.com." to "http://domain.com" with Nginx?
What's the recommended way of doing this? Regex or is there any other options?
The following snippet does this in a general way, without having to hard code any hostnames (useful if your server config handles requests for multiple domains). Add this inside any server definition that you need to.
if ($http_host ~ "\.$" ){
rewrite ^(.*) $scheme://$host$1 permanent;
}
This takes advantage of the fact (pointed out by Igor Sysoev) that $host has the trailing dot removed, while $http_host doesn't; so we can match the dot in $http_host and automatically use $host for the redirect.
You will need to use Regex.
server {
listen 80;
server_name domain.com.WHATEVER, domain.com.WHATEVER-2, domain.com.WHATEVER-3;
rewrite ^ $scheme://domain.com$request_uri? permanent;
}
From: http://wiki.nginx.org/HttpRewriteModule
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
permanent - returns permanent redirect with code 301