Let’s say you have a WordPress site with domain name mysite.com and you would like to change your domain name to mynewsite.com. Change the domain name is easy, but this should be done correctly or you will lose all of your SEO power.

Using 301 redirects with Nginx is the best way to accomplish the change of address.

Here is the 301 redirect code, you should but it in your server block directly after declaring your server name.

rewrite ^ $scheme://www.example.com$request_uri permanent; 1 rewrite ^ $ scheme : //www.example.com$request_uri permanent;

You have to add the $request_uri to the end of the domain or else Nginx will not be able to redirect any subpages.

If you want all links to redirect to the homepage (which might come in handy if you are switching to a single page parallax style site) use this.

rewrite ^ $scheme://www.example.com permanent; 1 rewrite ^ $ scheme : //www.example.com permanent;

Here’s an example Nignx server block for WordPress courtesy of DigiMix Web Design New York.

server { #listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 root /var/www/webdesign.com/html; index index.html index.htm index.php; # Make site accessible from http://localhost/ server_name http://webdesignnyc.com; rewrite ^ $scheme://webdesign.com$request_uri permanent; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ /index.php?q=$uri&$args; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 server { #listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 root / var / www / webdesign . com / html ; index index . html index . htm index . php ; # Make site accessible from http://localhost/ server_name http : //webdesignnyc.com; rewrite ^ $ scheme : //webdesign.com$request_uri permanent; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try _ files $ uri $ uri / / index . php ? q = $ uri & amp ; $ args ; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules }

By following the official Google change of address Guidelines, you can successfully change your website URL without damaging your search engine rankings.

Matt Cutt’s has put together this nice little video to explain how 301 redirects work with search engines.





References

Nix Craft. http://www.cyberciti.biz/faq/unix-linux-bsd-nginx-rewrite-old-domain-to-new-domain/

Google Webmasters. https://support.google.com/webmasters/answer/83106?hl=en