Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My Nginx setting currently has this:

 location / {
       if (!-e $request_filename){
            rewrite ^/(.*)$ https://domain.com/index.php?id=$1 redirect;
       }
 }

Basically for non-existing pages (404) it redirects user to the home page. But now I have a wordpress blog setup at https://domain.com/blog/, but any wordpress items eg. https://domain.com/blog/test also got redirected to the home page. I wonder how to fix this?

share|improve this question

2 Answers 2

If your blog is at http://yourblog.com/blog, then you need to set the directive to that path, unless you've previously specified that as your root directory (e.g. root /var/www/html/blog).

You should also NOT be using if statements. It's a common NGiNX pitfall.. Use try_files instead.

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


If you don't want sub-directories of /blog to be redirected, you can limit the redirect to just that specific directory by using the = in the location directive.

E.g.

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


If you want it to search for anything that says "blog", and apply that same redirect, you would use a / before and after, so that it uses it as a regular expression.

E.g.

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

More information on the location directive can be found HERE.

share|improve this answer

Based on the question/answer here and the documentation for location here:

there is no syntax for NOT matching a regular expression. Instead, match the target regular expression and assign an empty block, then use location / to match anything else.

I would say that you need to have a location directive that matches /blog/ and with an empty block to prevent the redirection, something like

location /blog {
}
share|improve this answer
    
There should not be a trailing slash after blog. –  nojak Nov 20 '12 at 23:36
    
@nojak fixed, thanks –  DerfK Nov 20 '12 at 23:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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