I've changed the domain name from long-domain-name-example.com to example.com which is shorter and easy to remember. How do I redirect old domain to new domain with "HTTP/1.1 301 Moved Permanently" status code under nginx web server running on Unix like operating systems?
| Tutorial details | |
|---|---|
| Difficulty | Intermediate (rss) |
| Root privileges | Yes |
| Requirements | Nginx |
Nginx web server comes with module called HttpRewriteModule. Use this module to change URI using regular expressions (PCRE), and to redirect and select configuration depending on variables. All you have to do is capture old domain and redirect to new domain. Edit /usr/local/nginx/conf/nginx.conf or /etc/nginx/nginx.conf, run:
# vi /usr/local/nginx/conf/nginx.confAppend the following line in server{ ... } block after declaring server_name:
rewrite ^ $scheme://www.example.com permanent;
In this case all requests to the uri such as long-domain-name-example.com, long-domain-name-example.com/file.html, long-domain-name-example.com/folder/file2.php, long-domain-name-example.com/cgi-bin/register.cgi?action=1 all are redirected to www.example.com with code 301. However, you may want to copy and pass the URL too, try:
rewrite ^ $scheme://www.example.com$request_uri permanent;
The $request_uri variable is equal to the original request URI as received from the client including the args. In other word redirect url as follows with 301 status code:
http://long-domain-name-example.com to http://www.example.com http://long-domain-name-example.com/file.html to http://www.example.com/file.html http://long-domain-name-example.com/cgi-bin/register.cgi?action=1 to http://www.example.com/cgi-bin/register.cgi?action=1 http://long-domain-name-example.com/static/js/lib.js?v=2 to http://www.example.com/static/js/lib.js?v=2
Sample code block:
### redirect beta.cyberciti.biz$URI to www.cyberciti.biz$URI with 301 ### server { listen 75.126.153.206:80; server_name beta.cyberciti.biz; root /usr/local/nginx/html; index index.html; rewrite ^ $scheme://www.cyberciti.biz$request_uri permanent; # .... }
Save and close the file. Reload the nginx web-server, run:
# /usr/local/nginx/sbin/nginx -s reload
redirect vs permanent flag
The permanent flag redirect URL with http status code 301. It means that a page has permanently moved to a new location. This code is recommended by search engine giant Google:
301 redirects are particularly useful in the following circumstances:
- You've moved your site to a new domain, and you want to make the transition as seamless as possible.
- People access your site through several different URLs. If, for example, your home page can be reached in multiple ways - for instance, http://example.com/home, http://home.example.com, or http://www.example.com - it's a good idea to pick one of those URLs as your preferred (canonical) destination, and use 301 redirects to send traffic from the other URLs to your preferred URL. You can also use Webmaster Tools to set your preferred domain.
- You're merging two websites and want to make sure that links to outdated URLs are redirected to the correct pages.
The redirect flag returns temporary redirect with http status code 302. It means that a page has moved temporarily to a new location. This code is not recommended for domain change.
References
- HttpRewriteModule module documentation from the nginx wiki.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 0 comments… add one now }