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.

let's say I have a site http://domain/ and I put some files in a subdirectory /html_root/app/ and I use the following rewrite rule to rewrite this folder to my root:

location / {
    root /html_root;
    index index.php index.html index.htm;

    # Map http://domain/x to /app/x unless there is a x in the web root.
    if (!-f $request_filename){
        set $to_root 1$to_root;
    }
    if (!-d $request_filename){
        set $to_root 2$to_root;
    }
    if ($uri !~ "app/"){
        set $to_root 3$to_root;
    }
    if ($to_root = "321"){
        rewrite ^/(.+)$ /app/$1;
    }

    # Map http://domain/ to /app/.
    rewrite ^/$ /app/ last;
}

I know this is not a clever way becase I have another subdirectory /html_root/blog/ and I want it can be accessed by http://domain/blog/.

My problem is, the above rewrite rule works ok but still have some issues: If I access

http://domain/a-simple-page/ (It's rewrited from http://domain/app/a-simple-page/)

it works fine, but if I access

http://domain/a-simple-page (without trailing slash), it redirects to original address:

http://domain/app/a-simple-page/,

Any way to redirect the URL without trailing-slash follow my rule?

share|improve this question

1 Answer 1

Classic case of following a down right wrong tutorial instead of reading the wiki I highly suggest reading about the features you (should) use (such as location and try_files) as well as my Nginx primer as you completely miss the basics of Nginx.

I have made an attempt to write what you want in a proper format but I cannot promise it'll work as I'm not sure I actually understand what you're trying to do, nevertheless, it should give you a basis to start from.

server {
    listen 80;
    server_name foobar;

    root /html_root;
    index index.php index.html index.htm;

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

    location /app {
        # Do whatever here or leave empty
    }

    location @missing {
        rewrite ^ /app$request_uri?;
    }
}
share|improve this answer
    
wget localhost/a-simple-page --2011-02-07 10:37:46-- localhost/a-simple-page Resolving localhost... 127.0.0.1 Connecting to localhost|127.0.0.1|:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: localhost/app/a-simple-page [following] --2011-02-07 10:37:46-- localhost/app/a-simple-page Maybe: rewrite ^ /app$request_uri/; –  alvosu Feb 7 '11 at 10:39
    
@alvosu not sure where you're getting that. There's no 301 redirect in my configuration. –  Martin Fjordvald Feb 7 '11 at 10:45
    
Well It's seems not working, and I got an error 403 on root (http://domain/) too. –  sparanoid Feb 7 '11 at 10:47
1  
@Sparanoid I don't know how your backend behaves but there's nothing in my actual configuration to cause that. But as I wrote in my answer you need to study Nginx, you cannot just install it and expect it to work. And with the amount of information you've provided here there's no one who can help you. –  Martin Fjordvald Feb 7 '11 at 10:51

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.