0

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/)(.+);
1

3 Answers 3

1

I recently migrated some Stacey sites to Nginx using this configuration:

server {

        root SERVER FILE LOCATION;
        index index.php index.html index.htm;

        # Make site accessible from
        server_name YOUR IP OR DOMAIN NAME;

        location / {
        # Stacey Rules
        error_page 404 /404.html;

        # Rewrite any calls to /render to the image parser
        rewrite ^/render/. /app/parsers/slir/ break;

        # Rewrite any file calls to the public directory
        try_files $uri $uri/ /index.php?$uri;
        }

        # Block access to .htaccess files
        location ~ /\.ht {
        deny  all;
        }

        include /usr/local/nginx/conf/staticfiles.conf;
        include /usr/local/nginx/conf/php.conf;
}

Note: the include files are just standard error page links and PHP (Fast-cgi php-fpm) config I've put together to save repeating the same thing across multiple site setups.

Via https://github.com/kolber/stacey/issues/143

0

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.

3
  • Thanks Michael but it gives me error. I have updated my question. Can you look at it?
    – borannb
    Jan 26, 2013 at 19:05
  • You haven't actually specified something to rewrite to there. Jan 26, 2013 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, 2013 at 20:13
0

I'm using stacey cms, here's what I'm using for nginx config in place of apache:

server {

    listen 80;
    server_name localhost;
    root /home/vagrant/test;
    index index.php index.html index.htm;
    error_page 404 /404.html;

    location / {
      if ($query_string != "") {
        return 301 $scheme://$http_host$query_string;
      }
      try_files $uri $uri/ /index.php?$uri;
    }

    location ~ \.php$ {
        try_files $uri =403;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

if if block is definitely not ideal or idiomatic with nginx configs. Necessary here for working with stacey tho.

You must log in to answer this question.

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