301 Redirect on nginx - nginx

I'm doing a 301 redirect from old domains to the new domain but can't able to exactly make it work.
So just a TLD change and everything else is same.. Like URL
Old domain : https://donateers.com
New domain https://donateers.org
Here is the code I tried in the nginx file but got 503 error.
server {
. . .
server_name donateers.com;
rewrite ^/(.*)$ https://donateers.org/$1 permanent;
. . .
}
The site is using the easy engine stack
Thanks
Suresh

Solution:
The above code works, Just I need to add in the main.conf file
Just below the code of
server_name donateers.com;
I need to add the following 1 line of code to do the 301 redirect.
rewrite ^/(.*)$ https://donateers.org/$1 permanent;
We're making the redirection permanent here and every URL of old domain change to the new URL with 301 redirection. Even works with http, www version of donateers.com
After you add this.. You need to restart the start with the one line of command.
ee site restart donateers.com
EasyEngine makes it very handy.

use this
server {
listen 80;
listen 443;
server_name .donateers.com;
return 301 $scheme://donateers.org$request_uri;
}

Related

Nginx Mess (URL Redirects)

I'm looking to redirect old urls to the new urls / new links. It's proving to be a mess to do just that. I'm using nginx 1.19.6 and ubuntu 18.04
I want to setup my redirects so https://ikhnetworks.com/feeds/Live/* would redirect to https://ikhnetworks.com/feeds/HD1/
https://ikhnetworks.com/Stations/* -> redirect to the respective call letters - for example
https://ikhnetworks.com/Stations/Radio/WDJO/ > https://ikhnetworks.com/WDJO/
For the first part, I can provide you the solution as:
server{
listen 443;
server_name ikhnetworks.com;
...
...
root /var/www/html;
location /feeds/Live {
return 301 https://ikhnetworks.com/feeds/HD1/;
}
}

Nginx - Redirect only main domain and not subdomain to WWW

How can I setup a redirection in Nginx conf to only redirect the main domain and not subdomain to WWW using $host or $server_name?
Any help will be appreciated.
server {
server_name your_domain_without_www;
location / {
rewrite ^(.*)$ http://your_domain_with_www$1 permanent;
}
}
server {
server_name *.your_domain_without_www;
...
}
You could also split the second server section into 2 separate ones if you need to handle www.your_domain and other subdomains of your domain differently.
Use .htacess file to redirect the url this will solve your problem

nginx url rewriting between 2 domain names

Got an issue with URL rewrite under nginx. Actually I have a main domain name dns1.com which is pointing a webserver with a WordPress installation (permalink = /%category%/%postname%/). I've created this page dns1.com/forum/forumname1.
I also have a domain name forumname1.com and I want this one to be pointed to the content of dns1.com/forum/forumname1. The issue is that I don't know how to display the result of dns1.com/forum/forumname1 with only forumname1.com in the bar address :( Actually it works when I go to forumname1.com/forum/forumname1 but this URL is pretty redundant :/ I should make this parameter "/forum/forumname1" hidden but how?
Here is my nginx serverblocks :
server{
server_name dns1.com;
root /var/www/dns1.com;
location = /forum/forumname1 {
rewrite ^/(.*)$ http://forumname1.com permanent;
}
}
server{
server_name forumname1.com;
root /var/www/dns1.com;
location = / {
### Rewriting rule HERE but which one?
}
}
Thank you :)

Handle index.php and non-www in nginx

I have a nginx config file that currently handles rewriting of requests via index.php for a Zend Framework application. Example code as follows:
server {
listen 80 default_server
etc....
rewrite ^/index\.php/?(.*)$ /$1 permanent;
try_files $uri #rewriteapp;
location #rewriteapp {
rewrite ^/(.*)$ /index.php/$1 last;
}
etc....
}
However, I now need to add a redirect so that any non-www requests for any domain are processed with a 301 to the www equivalent.
I've come across examples such as:
server {
listen 80;
server_name ~^(?!www\.)(?<domain>.+)$;
return 301 $scheme://www.$domain$request_uri;
}
But how would I combine everything so that any non-www are first redirected with a 301 and then rewritten via index.php?
Two separate server blocks (exactly as you have in your question). The first server block is the default, which handles the www prefix domains already, and (presumably) handles the index.php rewrite correctly.
The second server block only matches domain names without the www prefix.
If you wanted to do it the other way around, that is rewrite index.php before you redirect, then just add a rewrite line to the new server block.

nginx 301 rewrite root domain works but files are not being redirected

I'm trying to redirect a domain to another url in nginx and it works partially.
It looks like this:
location / {
rewrite ^/(.*) http://www.some_domain.tld/some_dir/some_file.php permanent;
}
It redirects fine the root domain, however if I try to load http://www.my_old_domain.tld/some_file.php it won't do the same. It'll load the page.
Thoughts ?
Thanks.
Try using this
location /
return 301 http://newdomain.com/a/b/c.php;
}

Resources