14

So, yesterday I had a question how to install the wordpress in the "/root" directory. I wasn't very successful in that one and I forgo'ed on that one.

So, right now the wordpress is located under "/var/www/wordpress" (so it's under separate folder) folder (I use Ubuntu 12.04 LTS, if it matters).

And my problem is that right after I had configured everything (everything was working like a charm there) I was redirected to the admin panel page. Everything was working fine up until the moment when I tried to visit the blog.

The URL address for my blog is: "blog.mysite.com". That is why I use NGINX, because I have two different applications (and environments) on one server and I need to distinguish between them.

So, the fact is: blog.mysite.com/wp-admin (/wp-login.php) is working totally OK, but when I visit the front page: blog.mysite.com, it keeps telling me that there is an endless redirect loop (301 redirect according to nginx access log file).

In admin panel I have both "WordPress Address (URL)" and "Site address (URL)" set to: "http://blog.mysite.com". Modifying either of them to something else, like: "http://blog.mysite.com/wordpress" is not helping at all!

".htaccess" file is empty, but I'm using default permalinks, so should not be a problem (However, I'm not sure).

Both nginx and apache2 root directives are pointing to "/var/www/wordpress". Static files (css,js) are working, if it matters.

How can I fix this problem ? Any help is much appreciated!

Thank you in advance!

3
  • did you figure this out?
    – ark
    Jul 4, 2017 at 12:52
  • @ark Hi. Yes. It turned out that our WP installation was faulty, so it wasn't the nginx issue after all :( If you have something similar, try re-installing or installing a clean blog next to your existing one and see if that works first before you start touching the nginx.
    – Dmitri
    Jul 4, 2017 at 15:03
  • same problem by other solution : wordpress.stackexchange.com/a/220896/127528
    – sj59
    Dec 2, 2020 at 13:24

7 Answers 7

16

Not the prettiest fix, but removing the redirect filter in your theme functions.php file worked for me.

remove_filter('template_redirect', 'redirect_canonical');

From the link provided in OP's answer:
Wordpress did infinite 301 redirect loop

4
  • 2
    holy crap. 40 hours later, i finally have an answer and it's one freakin' line.
    – osoblanco
    Apr 7, 2016 at 2:18
  • 1
    Welcome to wordpress tinkering! Apr 7, 2016 at 2:35
  • 1
    That's genius. Spent half day trying to figure out through nginx logs. Dec 5, 2019 at 6:43
  • Same here wasted whole day and this was it just one line. oh man i hate wordpress when this happen
    – AHSAN KHAN
    Sep 14, 2020 at 11:42
9

Finally, found a solution:

http://www.violato.net/blog/php/88-wordpress-did-infinite-301-redirect-loop

Hope this will help others that have the same problem as I did.

Thanks everyone.

2
  • This is not loading anymore. If you have the solution on your end, can you please edit your answer and add them directly? Thanks
    – Arda
    Oct 3, 2014 at 13:02
  • 1
    The link outline's Emile's response included here. Poor formatting but in case its redacted, add the following to your theme's functions.php file: remove_filter('template_redirect', 'redirect_canonical');
    – DevOops
    Mar 14, 2017 at 15:13
7

I had a similar problem using Nginx as a reverse proxy for Apache.

After a few hours I found out it was caused by the $_SERVER["REQUEST_URI"] being set to index.php by Nginx instead of the actual url and Wordpress was trying to remove index.php by redirecting to the url without index.php in wp-includes/canonical.php.

The solution for me is using something like this,

proxy_pass http://111.111.111.111:8080$request_uri;

So adding the $request_uri fixed it.

2

I had a lot of problems when switching from Apache to Nginx in the past, all solved when I purged Apache, which somehow was interfering with Nginx and caused problems to every server. Here is my wordpress configuration for Nginx, according to both Nginx and Wordpress guides for each other:

server {
    listen 80;
    server_name blog.mysite.com;

    root /var/www/wordpress;
    index index.php;

    charset utf-8;

    location / {
      try_files $uri $uri/ /index.php?$args;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;
    }

    location ~ \.php$ {
        try_files $uri /index.php;

      fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # CHANGE THE LINE ABOVE IF NEEDED
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

I didn't even have to change anything in the admin panel when switching servers, it just worked fine.

1
  • Everything I have on my VPS: PHP (php5, php5-mysql, libs), Apache2 - is a fresh install. They were pre-installed on the VPS, but I purged both of them and made a fresh install. So, I clearly don't understand what is going on there and why this redirect occurs in the first place (Considering the fact that the admin panel is working flawlessly) But, many thanks! BTW, I don't use "fastcgi" directive, I just proxy pass it to Apache: proxy_pass 127.0.0.1:8081 (I have defined the port by myself)
    – Dmitri
    Nov 14, 2013 at 15:02
2

In my case, the redirect loop only affected wp-admin pages, so I added at the start of the line

if (is_admin) remove_filter('template_redirect', 'redirect_canonical');

and still works, plus the redirect canonical keeps doing its job on the front end!

I hope this helps someone else!

0

I was having this issue because I was using nginx to forward the request, but nginx wasn't preserving the HOST header.

Once I added these values under my location block things started working as expected.

    # Set proxy headers for the passthrough
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # Let the Set-Cookie header through.
    proxy_pass_header Set-Cookie;
0

This is a solution for nginx with wordpress docker
The key point is to add "host" and "proto" to proxy

location / {
    try_files $uri @apache;
}

location @apache {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
    proxy_pass http://wordpress:80;
}

references: https://azdigi.com/blog/en/website-tutorials/wordpress-en/how-to-install-wordpress-with-docker-compose-nginx-apache-and-ssl-2/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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