Nginx redirect with subdomain and path - nginx

Using Nginx I want to redirect from a list of old URLs to new.
I have a huge list like so.
few examples
https://subold.domain.com/old-path-0 -> https://subnew.domain.com/new-path
https://subold.domain.com/old-path-1 -> https://subnew.domain.com/new-path-alpha
https://subold.domain.com/old-path-2 -> https://subnew.domain.com/new-path-gamma
https://subold.domain.com/old-path-3 -> https://subnew.domain.com/new-path-2

In your old domain configuration:
rewrite ^/old-path$ http://subnew.domain.com/new-path permanent;

Related

Rewrite URL's to Subdomain in 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.

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;

NGINX Subdirectory Redirect is not working

TLDR: subdirectory/cool redirects to newdirectory/cool. subdirectory?cool incorrectly redirects to newdirectory without the parameter.
The Prob:
I'm attempting to redirect a subdirectory to a new directory while preserving the original URL parameters via NGINX location using the regex ~* ^(?:((\/m\b)(?!-)(\/)?))(?<requestedPath>.*)?$
The paths such as /subdirectory/cool successfully redirect to /newdir/cool. However, paths like /subdirectory?cool and /subdirectory/?cool are redirecting to /newdir without preserving the URL parameters.
The redirect should work as follows:
/subdirectory -> /newdir
(Many of the suggestions I've seen posted here for subdirectrory redirects assume a trailing slas or do not take into account that their regex for /m also impacts /media)
/subdirectory/ -> /newdir/
/subdirectory/test -> /newdir/test
/subdirectory/?apple -> /newdir/?apple
/subdirectory?apple -> /newdir?apple
The test is successful here: Successful Test
The code that is failing:
location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
return 301 /newdir/$requestedPath;
}
Additionally, I have also tried using rewrite instead of location to no avail.
Apparently, with location, one has to us $is_args.
This did the trick: return 301 /newdir/$requestedPath$is_args$args;
End result:
location ~* ^(?:((\/subdirectory\b)(?!-)(\/)?))(?<requestedPath>.*)?$ {
return 301 /newdir/$requestedPath$is_args$args;
}

Nginx rewrite simple issue

I've migrated my site and now got a huge list of links that I need to redirect to their corresponding new urls. I'm not a developer or sys admin and can't really help myself with the Nginx manuals.
A short snippet of my list looks like this:
rewrite /?tn=productview&c=34223&pid=8965214 /url1 permanent;
rewrite /?tn=productview&c=54424&pid=6180497 /url2 permanent;
rewrite /?tn=productview&c=54426&pid=2563446 /url3 permanent;
rewrite /?tn=productview&c=54426&pid=6889674 /url4 permanent;
The problem is that Nginx will in that particular case redirect the first line correctly and all following lines to "url1".
How should I write this so that each of this links be redirected to their corresponding new url?

Resources