Rewrite URL's to Subdomain in NGINX - nginx

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.

Related

NGINX Rewrite url subdomain to maindomain with variable

I should rewrite url
subdomain.test.com/login?xxxxxx
to
test.com/login?xxxxxx
how can I configure try files part for that?
.htaccess wont possible

Redirect NGINX with # in location

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?

Nginx rewrite rule for changing domain name in image urls

I have Wordpress website which is live. In this website are a lot of posts with uploads over 200GB. I want to create in localhost development environment which I have done, but I have problems with posts images, because I don't want to download all of them and put in my localhost. So I am wondering if there is any Nginx rewrite rule which will rewrite only domain name in my image urls.
I need to rewrite this url:
https://example.local/wp-content/uploads/2020/10/10/very-nice-picture.png
To this one:
https://example.com/wp-content/uploads/2020/10/10/very-nice-picture.png
This is possible with Nginx or there is other better solution?
Here are your options:
Use a redirection:
location /wp-content/uploads/ {
return 301 http://example.com$request_uri; # or use 302 to prevent redirection caching
}
Use transparent proxying:
location /wp-content/uploads/ {
proxy_pass http://example.com;
}
You can also use remote files only in case they are missing on the local development server:
location /wp-content/uploads/ {
try_files $uri #remote;
}
location #remote {
# redirect or proxy the request as shown above
}

NGINX Redirect www.website.com/folder/ to website.com

Recently migrated a website out of multisite network(used ubuntu). Website was placed in subfolder and now is the exact, but root domain. How to change/redirect existing links, excluding /folder/ part?
If you want a 301 HTTP redirection, you can try
rewrite ^/folder(/.*) $1 permanent;
before the first location block.

Wordpress - How to create redirection in NGINX?

I have a dilemma. I migrated my wordpress site from Apapache server to NGINX.
In the process I changed permalink in WP from
/index.php/%postname%/
to
/%postname%/
Now, users coming to site from Google, are getting 404's because of the permalink change. Typically I would just redirect any page via WP plugin, but because of this index.php in the permalink, plugins don't work. So I have no choice but to create a redirection somewhere in NGINX conf file.
Please advise what to do.
server {
rewrite ^/index.php/(.*)$ /$1 permanent;
...
}
In the server configuration file (the file will be located at /etc/nginx/nginx.conf).
If it does not exist there, it may also be at /usr/local/nginx/conf/nginx.conf or /usr/local/etc/nginx/nginx.conf.
For temporary redirect:
rewrite ^/oldlocation$ http://www.newdomain.com/newlocation redirect;
For permanent redirect:
rewrite ^/oldlocation$ http://www.newdomain.com/newlocation permanent;

Resources