1

I deployed my wordpress website on NginX successfully. I need permalinks to be http://www.example.com/%postname% and blog to be http://blog.example.com/%postname%. It redirects all www traffic to non-www but when I try to configure it to redirect non-www to www by uncommenting the lines in mydomain config file for NginX written below, the browser complains of redirect loop.

My global/restrictions.conf is:
http://pastebin.com/cLfMv6jC
and global/wordpress.conf is:
http://pastebin.com/Uc9KSqhh

and mydomain config file for NginX with server blocks are:

server {
    server_name  _;
    return 301 $scheme://www.example.com$request_uri;
}

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

server {
    listen 80 default_server;
    listen [::]:80;

    root /usr/share/nginx/html/example.com;
    index index.php index.html index.htm;

    server_name www.example.com;

    include global/restrictions.conf;
    include global/wordpress.conf;

}

Kindly, point out my mistake.
[Update] As a matter of fact, I also have the following server block:

server {
    listen 80;
    listen [::]:80;
    root /usr/share/nginx/html/mydomain_test;
    index index.php index.html index.htm;

    server_name test.example.com;

    include global/restrictions.conf;
    include global/wordpress.conf;

}
1

I am confused as to why you use server_name _. This used to be used for the designate a default sever but then you set server_name www.mydomain.com stanza as the default (listen 80 default_server).

I would rewrite your configuration this way:

server {
    listen 80;
    listen [::]:80;

    server_name www.mydomain.com;

    root /usr/share/nginx/html/mydomain.com;
    index index.php index.html index.htm;

    include global/restrictions.conf;
    include global/wordpress.conf;

}

server {
    listen 80 default_server;
    server_name mydomain.com;
    return 301 $scheme://www.mydomain.com$request_uri;
}

If the modifications above do not work, I would focus on your includes. Isolate them by creating a simple /usr/share/nginx/html/mydomain.com/test.html and modifying the first stanza:

server {
    listen 80;
    listen [::]:80;

    server_name www.mydomain.com;

    root /usr/share/nginx/html/mydomain.com;
    index test.html

}

Then, test by visiting www.mydomain.com and domain.com and see if both direct you to the test.html you created. If they do, then dig into those includes.

| improve this answer | |
  • The config you suggested also causes redirect loop errors when accessing the mydomain.com. I needed server_name _ to make permalinks http://www.mydomain.com/%postname% work. Otherwise, if I remove that, the home page is accessible but other pages causes redirect loops. – Shah-G Jul 7 '14 at 21:36
  • I've tried test.html and the redirection works smoothly even with global/restrictions.conf and global/wordpress.conf included. I figured out that a change in wordpress admin Settings was needed with the WordPress Address (URL) and the Site Address (URL) changed to http://www.mydomain.com. With this change, permalinks work fine without server_name _ and I also do not need the redirecting server block server_name mydomain.com; – Shah-G Jul 8 '14 at 3:34
  • Okay. Suppose the removing of the includes wasn't necessary but just being thorough. Glad it worked! – Belmin Fernandez Jul 8 '14 at 8:58
  • The Wordpress Address and Site Address settings are.... stored as siteurl and home in wp_options. – Some Linux Nerd Jan 28 '17 at 0:02
-2

Your wordpress installation is also capable of redirecting a visitor to what it thinks is it's correct url. It does this by specifying a Location: header with the php header() function. So what you might be seeing is your site redirecting to where you told nginx to go, and then wordpress sends it back.

In your wp_options table, set the options named "siteurl" and "home" to the www.domain.com uri.

| improve this answer | |
  • Did I mention that wordpress will only let you use one URL? If not, you should be aware that wordpress will only let you use one URL. It's a really annoying bit of functionality. – Some Linux Nerd Jul 7 '14 at 23:43
  • (This was correct btw, it comes up repeatedly if you ever work for a webhost.) – Some Linux Nerd May 10 '16 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.