1

In the past a project was getting moved from / to /subdir. As the old URL structure was something like /index.php?foo/123/bar I used the following rewrite to get everything to /forum:

rewrite ^\/index\.php(.*)$ /forum/$1 last;

The problem with this rewrite is that it also redirects / to /forum/ and /index.php to /forum/. Now I want to not redirect them because there has been created a new start page in /.

So / should not be redirected but if there is a query string it should.

I tried the following:

location / {
        if ($query_string !~ "") {
                rewrite ^\/index\.php(.*)$ /forum/$1 last;
        }

        try_files $uri $uri/ @rewrite;
}

The start page works now but if there is a query string nothing gets rewritten and I don’t know why.

1 Answer 1

3

After half a day of silence I figured it out. By using the full $request_uri outside the location block and an adjustment to the regex to make any character after the index.php mandatory it works fine:

location / {
    try_files $uri $uri/ @rewrite;
}

if ($request_uri ~* "^\/index\.php(.+)$") {
    rewrite ^(.*)$ /forum/$1 last;
}

You must log in to answer this question.

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