nginx redirect to www.domain

177

Solution 1

You're doing it the hard way. Here's the easy way.

server {
    listen 80;
    server_name  example.com;
    rewrite ^(.*) http://www.example.com$1 permanent;
}

server {
    listen 80;
    server_name  www.example.com;
    #The rest of your configuration goes here#
}

Solution 2

It's better to use return instead of rewrite, because it's faster.

server {
    listen 80;
    server_name     example.com;
    return          301 http://www.example.com$request_uri;
}

server {
    listen 80;
    server_name     www.example.com;
    [...]
}

This way, we also send the client a proper status code, so that he asks the right domain in the next request.

Share:
177

Related videos on Youtube

Jolly Priya
Author by

Jolly Priya

Updated on September 18, 2022

Comments

  • Jolly Priya
    Jolly Priya over 1 year

    Here i want to compile/run first ExceuteLeadTest.class and then ExceuteContactTest.class by the ant ..But while exceuting it first it comipling/running ExcecuteContactTest.java/ExceuteContactTest.class

    <project>
    <target name="run" depends="compile">
    <include name="testScript/ExceuteLeadTest.class" />
    <include name="testScript/ExceuteContactTest.class" />
    </target>
    <project>

  • ceejayoz
    ceejayoz about 11 years
    Gave you an upvote, but thought I'd note that rewrite permanent sends a 301, too.
  • Jolly Priya
    Jolly Priya about 6 years
    can u please explain in detail as i am new to ANT..where to write the above tags in build.xml?
  • pamcevoy
    pamcevoy about 6 years
    I added an example of one target calling two other targets.
  • Pedro Lobito
    Pedro Lobito over 3 years
    Shouldn't server have the closing tag } ?