1

I just migrate from Apache to Nginx and trying to set up rewrite rules. I have a virtual host and this is the structure of virtual host

/var/www/name-of-virtual-host/
├── wp1
│   ├── wp-admin
│   ├── wp-content
│   └── wp-includes
└── wp2
    ├── wp-admin
    ├── wp-content
    └── wp-includes

As you see I have 2 different WordPress installations in different folders. So I'm trying to create a rewrite rule to work with these 2 WPs.

This is my location definition:

        location / {
                try_files @missing $uri;
        }
        location @missing {
                rewrite ^/([a-z-])(/.*)$ /$1/index.php last;
        }
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

If URI is /wp1/permalink-of-post/ it should use /wp1/index.php and if /wp2/permalink-of-post/ it should use /wp2/index.php .

But I'm getting rewrite or internal redirection cycle while internally redirecting to error.

How can I write my rewrite rules to use with multiple WP installations ?

UPDATE 1

An example WordPress .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp1/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp1/index.php [L]
</IfModule>

# END WordPress
3
  • ping. did you got your issue resolved? If the answers helped, could you please make sure to award the bounty amount you've promised?
    – cnst
    Sep 22, 2015 at 2:42
  • 1
    I must say that it's rather impolite and treacherous to offer a bounty, and then fail to award it without providing a single comment. What were you looking for that's still missing from the answers?
    – cnst
    Sep 23, 2015 at 8:16
  • @cnst , you are right for every word you said. It's my mistake, because I had to pause this project a few week and was busy with another thing (changed my job). So I couldn't try answers quickly. I was thinking reopen bounty and give it to an correct answer. But I see it's too late to do this. I'm sorry about this, it's my mistake.
    – Eray
    Oct 9, 2015 at 14:07

2 Answers 2

4
+25

Isn't your try_files in the wrong order? Perhaps that's what causing you the headache and the endless cycles, because the file @missing, well, is indeed missing from your filesystem, and redirecting back to $uri indeed must cause an endless cycle for sure?

-try_files @missing $uri;
+try_files $uri @missing;

Nonetheless, if you only have two WordPress sites, I think a better approach might be to simply have a little bit of copy-paste going on, instead of trying to invoke regular expressions and all each time.

E.g., would probably be better to have the following than trying to do lots of rewriting and multiple redirects:

location /wp1 {
    try_files $uri /wp1/index.php;
}
location /wp2 {
    try_files $uri /wp2/index.php;
}
2
  • I would say this is correct. From what I can tell is that at the moment, the try_files doesn't check if $uri exists first, but rather it goes to @missing immediately, resulting in a loop. For extra information, see also "How does try_files work?"
    – Sašo
    Sep 21, 2015 at 15:48
  • 1
    @Sašo, no, it doesn't ever go to the @missing location, either, it simply tries loading the file with the name @missing directly from the filesystem (e.g., $document_root/@missing), which is unlikely to exist, and then goes back to itself as $uri is never changed (and existence of $document_root$uri is never checked, either), forming an infinite loop through a single location, for every single request that's handled by the / location.
    – cnst
    Sep 22, 2015 at 6:29
1

This catches my attention straight off:

rewrite ^/([a-z-])(/.*)$ /$1/index.php last;

$1 in this case matches only one character.

I'm not going to go further into fixing this though. Rather than re-inventing the wheel you should start from a recommended configuration. There's just too many pitfalls and lessons to learn otherwise. http://codex.wordpress.org/Nginx

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .