Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


staceyapp clean urls in nginx
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

staceyapp clean urls in nginx

bboranbboran Member
edited January 2013 in Help

Hello mates. I am using stacey cms for my portfolio web site. It is running great but i need to enable clean urls for the cms. Normally it provides a .htaccess file for enabling them but i am using nginx so i couldnt get them work. Here is the content of the .htaccess file;

`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.

«1

Comments

  • Change to the apache and put the site online activate varnish and after that play with nginx and made the switch back .
    You will be surprise how well apache works with varnish thanks to tux.
    The guide is in the tutorials.

  • I dont want to use apache, sorry.

  • Even if you post to nginx forum, I doubt if you will get answer. Just google hard and try to get some nginx books.

  • It's not a perfect converter, but it does a pretty decent job converting .htaccess rewrite rules to Nginx ones.

    http://winginx.com/htaccess

  • I tried this tool. It doesnt give a decent output sorry. It also creates invalid arguments.

  • bboranbboran Member
    edited January 2013

    I have achieved some progress. Here is my rewrite codes;

    `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.

    i think i have found the problem. With this rewrite it adds a "./" in front of the link structure. For example;

    <link rel="stylesheet" href=".//public/docs/css/screen.css" type="text/css" media="screen">

    How can i fix this?

  • The ideea to switch to apache was only temporary until you fix clean url so in the meantime your site its up and running and you can work on your computer or another vps to fix nginx configuration.

  • tuxtux Member
    edited January 2013
  • IntcsIntcs Member
    edited January 2013

    IMO that's similar to having a feature missing in a script, then the question for me becomes whether or not it's worth trying to modify it, and if I did I actually can send my work to the author to officially add it in next releases. The same here if htaccess/Apache wasn't supported yet by author you might be able to send them that feature if you eventually got it to work by your own. That's why I'd consider first if there's other option to do the same thing, or if it's that much necessary , before wasting that much time.

    As some suggested, and as it's portfolio website so you're supposed to have a small traffic. In that case Apache can do with very small memory use, so can run on almost any VPS. So, I'd use Apache, with something like that in apache conf:

    <IfModule mpm_prefork_module> StartServers 1 MinSpareServers 0 MaxSpareServers 1 MaxClients 20 MaxRequestsPerChild 3000 </IfModule>

  • Intcs has right apache with proper config can do very well. Default tuxlite should do just fine.

  • Ok i switched to another lightweight cms which uses a very basic .htaccess. Can you help me to convert this to nginx format? Thanks.

    `RewriteEngine On

    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f

    RewriteRule ^(.*)$ ./index.php?request=$1`

  • IntcsIntcs Member
    edited January 2013

    Personally I'm not familiar with nginx format, and still using Apache mostly. I hope someone can help you with it.

    But why you didn't continue with what you've started for the first CMS, and perhaps search some help from nginx community?
    Anyhow if you find a list with flat file CMS useful, as I do have several of it.

  • Because the first cms was hard to use and modify. I am not a programmer or a web developer. I am a 3D Modeling Artist so i need a cms which is easy to use. I am very happy with the current one. It is created for designers and artists especially and very easy to use. And as you can see the htaccess file is very minimal. I hope ill find someone who can convert to nginx =D Thank you.

  • Ok let's wait if someone familiar enough with Nginx can do it.
    As for the flat-file CMS that's the list of some that I've tried, I recall some of them has an embedded WYSIWYG editor

    GuppY
    Nano CMS
    phpCMS
    Pluck
    CMSimple
    Quick CMS
    SkyBlueCanvas
    razorCMS

  • location / {
    if ($script_filename !~ "-d"){
    rewrite ^(.*)$ /index.php?request=$1;
    }
    }

    ?

  • bboranbboran Member
    edited January 2013

    This is the output from http://winginx.com/htaccess and it sucks, sorry.
    http://wiki.nginx.org/IfIsEvil

  • What actually makes you think someone would bother going through all those rules, converting them, testing them for nothing?

    The easy way is finding a freelancer experienced in regex and nginx syntax.

  • For nothing? What is the purpose of this forum? To help each other maybe? If someone asks a questions that i know the answer, I answer it without wanting anything. Also i am asking this in here because this is not a complicated htaccess rule as i think. Maybe someone can convert it for me.

  • Helping each other only works when you either have infinite time in your hands, or the task is dead simple.

    The type of people who are well versed in regex aren't likely to have the former ;)

    You can most likely get this done for $5 or something on Fiverr, just sayin'.

  • bboranbboran Member
    edited January 2013

    I looked at Fiverr but they are all starting with "I will" =) So does it allow you to give a job to someone?

  • EllimistEllimist Member
    edited January 2013

    It's simple enough.

    location / {
        try_files $uri $uri/ /index.php?request=$uri&$args;
    }
    
  • Thank you Ellimist but it didnt work. It didnt give any error but its not working. For example without clean urls the url is like that; domain.com/?page=works after i enable clean urls it makes url like this domain.com/works but it doesnt load the page. It shows the homepage always when clicking links.

  • bboranbboran Member
    edited January 2013

    By the way this is my whole site.com nginx conf;

    server {
    listen 80;
    #listen [::]:80 default ipv6only=on;

        server_name www.domain.com domain.com;
        root /home/user/domains/domaincom/public_html;
        access_log /home/user/domains/domain.com/logs/access.log;
        error_log /home/user/domains/domain.com/logs/error.log;
    
        index index.php index.html index.htm;
        error_page 404 /404.html;
    
        location / {
           try_files $uri $uri/ /index.php?request=$uri&$args;
        }
    
        location ~ \.php$ {
            try_files $uri =403;
            fastcgi_pass unix:/var/run/php5-fpm-user.sock;
            include fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    

    }

  • IntcsIntcs Member
    edited January 2013

    @bboran said: Thank you Ellimist but it didnt work. It didnt give any error but its not working. For example without clean urls the url is like that; domain.com/?page=works after i enable clean urls it makes url like this domain.com/works but it doesnt load the page. It shows the homepage always when clicking links.

    Are your pages named in other languages than english? If so try by giving them english names.
    I'd also contact them by support forum or email, to see if there's anything about converting urls.

  • bboranbboran Member
    edited January 2013

    I am using secretary cms not staceyapp. And my links are all english. What i did and achieved something. I changed the code like that;

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

    And this is now works but its not working properly. Now all the links domain.com/projects, domain.com/blog and domain.com/about points to domain.com/?page=projects

  • IntcsIntcs Member
    edited January 2013

    Well that's a quote from their faqs:

    Follow these steps to get Clean URLs working properly on your site.

    • In the site folder of your Secretary installation is a file called htaccess.txt. Move this file to the location of your website.
    • Rename it ".htaccess".
    • Now, go to the General Settings panel (Settings > General Settings) and check the "Clean URLs" box under the "Site Settings" heading. Click save.

    Have you enabled "clean URLs" option in settings?

  • IntcsIntcs Member
    edited January 2013

    @bboran said: I am using secretary cms not staceyapp.

    I see that this one requires Mysql, so you are running database and not seeking scripts running with a flat file anymore, if so, that gives you many better choices if you are interested!
    So, in case you didn't get it to work, then you can actually use Wordpress, and it's very easy to setup, then you can choose of that many plugins which are easy to add/install too, for turning it from regular blog to an image gallery/portfolio. You are able to choose which plugin suits you more, here http://wordpress.org/extend/plugins/search.php?q=image+gallery

  • Okay, the reason why the clean URLs isn't working is because Secretary CMS needs the $uri without a leading /. To demonstrate, this is why this works and this doesn't.

    However Nginx's $uri variable includes a leading slash.

    To overcome this, I used a mapped variable to get the $uri without the leading slash. There may be other methods to achieve this.

    map $uri $myuri {
        ~^/(?<catch>.*)$  $catch;
    }
    
    server {
        listen 80;
        #listen [::]:80 default ipv6only=on;
        server_name www.domain.com domain.com;
        root /home/user/domains/domaincom/public_html;
        access_log /home/user/domains/domain.com/logs/access.log;
        error_log /home/user/domains/domain.com/logs/error.log;
    
        index index.php index.html index.htm;
        error_page 404 /404.html;
    
        location / {
            try_files $uri $uri/ /index.php?request=$myuri;
        }
    
        location ~ \.php$ {
            try_files $uri =403;
            fastcgi_pass unix:/var/run/php5-fpm-user.sock;
            include fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    

    Regex in mapping pattern only works in v0.9.6+.

  • Ellimist waoww. Thank you very much. I think you have fixed the issue. I cant test it right now because i am at work right now. But after work ill try this and report back to you. Thank you for testing this and replying to me. That was a big problem for me.

  • Ellimist, now i have tried your code on my site and its is working without any problem. Really thank you very very much. You saved me alot. Just one question;

    Does this mapped variable method has any disadvantages on performance or security wise? It is safe to use right? Thank you again!

Sign In or Register to comment.