I know how to force and redirect www.example.com to example.com under Lighttpd web server. How do I force nginx web server to redirect www.example.com/page/1/2/3 to example.com/page/1/2/3?
You can easily redirect www to nowww domain for SEO and other purposes using the following syntax. Edit nginx.conf file, enter:
# vi /usr/local/nginx/conf/nginx.conf
To force www.theos.in/page.html to theos.in/page.html, enter:
### redirect www to nowww with client code 301 ### if ($host = 'www.theos.in' ) { rewrite ^/(.*)$ http://theos.in/$1 permanent; }
Save and close the file. Test and reload nginx webserver, enter:
# /usr/local/nginx/sbin/nginx -s reload
Fire a webbrowser and type the url:
http://www.theos.in/windows-xp/free-fast-public-dns-server-list/
It should be redirected to
http://theos.in/windows-xp/free-fast-public-dns-server-list/
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














{ 7 comments… read them below or add one }
if ($host = 'www.theos.in' ) {
rewrite ^ http://theos.in$uri permanent;
}
I believe $1 does the same thing as $uri, isn’t it?
out of interest, Vivek ..
why:-
/usr/local/nginx/sbin/nginx -s reloadas opposed to:-
/etc/init.d/nginx restart??
(
here’s the deal for redirecting to, say another site:-
server {listen 80;
server_name http://www.mydomain.org mydomain.org;
rewrite ^/(.*) http://www.mydomain.com/$1 permanent;
}
)
like those penguins. looks like London :P
Edited by admin – code tags added.
@the_guv,
reload will only load changed config option without disconnecting http clients. This is ideal for high traffic sites or reverse load balancers i.e. gracefully shutdown the old worker processes and reload changed config options. This is like sending HUP single to nginx.
restart will restart the webserver, close & reopen log file and disconnect http clients. This is not ideal. However, I’ve not seen /etc/init.d/nginx SysV script. As most my installation are done using source code or custom build rpms.
I suggest you take a look at the official page for more info for reload vs restart:
http://wiki.nginx.org/NginxCommandLine
Yes config can be different. My config make sure http client do not get into any loop with if conditional.
HTH
I appreciate that, Vivek .. and that link which I’d not seen.
.. think I’d best reconsider one or two commands on some Nginx tuts I’ve written up then!
Would like very much your opinion if you’ve the time, sometime. It is running at about 40 parts so far but then again there’s a detailed index to pin down anything of interest:-
Set Up an Unmanaged VPS (4 Newbies) .. The V-P-S Bible
.. I know that sounds a bit spammy. Genuinely good to build bridges with Nginx community members tho.
Great content you have here. I’ve skulked around this site quite a bit in the last year or so. Thank you.
@the_guv,
Nice work!
Generic version:
if ($host ~* www\.(.*)) { rewrite ^(.*)$ $scheme://$host_without_www$1 permanent; }