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.

Stacey is a lightweight content management system. I am using it on my porfolio website however it uses .htaccess file to make friendly urls. Is it possible to rewrite this .htaccess lines for using in nginx? Here is the .htaccess file content;

RewriteEngine on

# Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If      you are running from a subdirectory, your rewritebase should match the name of the path to    where stacey is stored.
# ie. if in a folder named 'stacey', RewriteBase /stacey
#RewriteBase /

ErrorDocument 404 /404.html

# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder      matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.|\?)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /render to the image parser
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]

# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]`

Thanks in advance!


I achieved some progress. Here it is;

location / {
if ($uri ~ "/app/$"){
rewrite ^/app/ /index.php last;
}

if (!-e $request_filename){
rewrite ^/(.*)/$ /index.php?$1 last;
}

if (!-f $request_filename) {
rewrite ^/(?!public/)(.+);
}

if (!-f $request_filename) {
rewrite ^/(?!public/)(.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ last;
}

if (!-e $request_filename) {
rewrite ^/(?!public/)(.+)$ public/$1 last;
}

This rewrite all the projects without any problem right now. For example;

domain.com/?/projects/project-name-3/ became domain.com/projects/project-name-3/

but there is also another 2 pages. one of is "about" and one of is "contact me" . they also became; domain.com/about and domain.com/contact-me but when i click them the page loads but without css styling. any clue?


okay new code is like this and it gives me error; nginx: [emerg] invalid number of arguments in "rewrite" directive in /etc/nginx/sites-enabled/domain.com:21

location / {
rewrite ^/app/ /index.php last;
}
if (!-e $request_filename){
rewrite ^/(.*)/$ /index.php?$1 last;
}

if (!-f $request_filename) {
rewrite ^/(?!public/)(.+);
}

if (!-f $request_filename) {
    rewrite ^/(?!public/)(.+)\.(html|json|xml|atom|rss|rdf|txt)$ $2/ last;
}

try_files /public$uri /public$uri/ $uri $uri/ /index.php;
}

the code in the 21. lines are;

rewrite ^/(?!public/)(.+);
share|improve this question
    
and also you can look at the file layout from projects github github.com/kolber/stacey –  borannb Jan 26 '13 at 11:04
add comment

1 Answer 1

You're most of the way there.

Three of the five rewrites can be replaced with nginx's much faster and easier try_files directive. One can be simplified a bit. The fifth has a logic error as posted in your question.

  1. First, you don't need to place this check in an if since it contains its own pattern match.

    location / {
        rewrite ^/app/ /index.php last;
    

    This could also be placed in its own location block, but leave that for later...

  2. Second, your logic error. When you rewrote this rule, you changed two () pattern matches to three, but didn't adjust the backreference $1 to $2 to account for its changed position in the new regex.

        if (!-f $request_filename) {
            rewrite ^/(?!public/)(.+)\.(html|json|xml|atom|rss|rdf|txt)$ $2/ last;
        }
    
  3. Finally, try_files. For very simple matches, this is much faster than rewrite and also easier to understand. So...

        try_files /public$uri /public$uri/ $uri $uri/ /index.php;
    }
    

This should get you about 99% of the way there, barring any last tweaks based on information not in your question.

share|improve this answer
    
Thanks Michael but it gives me error. I have updated my question. Can you look at it? –  borannb Jan 26 '13 at 19:05
    
You haven't actually specified something to rewrite to there. –  Michael Hampton Jan 26 '13 at 19:08
    
Cause i am not a programmer. I am a 3d modeler just want to use this lightweight cms to show my work. But i need clean urls. I am trying to get a complete answer to rewrite this htaccess to nginx. Asked this question in lots of place and didnt get a proper answer. Thank you for your answer but i think i have to give up =/ –  borannb Jan 26 '13 at 20:13
add comment

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.