Need help with converting this htaccess to nginx rewrite

Igor Sysoev is at rambler-co.ru
Sat Feb 14 14:49:30 MSK 2009


On Fri, Feb 13, 2009 at 10:59:23PM +0100, Brian Zerangue wrote:

> Sorry, I didn't realize that this was plain-text only... forgive me for 
> the HTML in the last post. Here's my post in plain-text.
> 
> 
> I'm really interested in utilizing Nginx on my Slicehost account, but my 
> main holdout is understanding how to adjust .htaccess files so that my 
> site can function properly on Nginx.
> 
> I've made an attempt, but I'm just not a regular expressions guy and am 
> new to this part altogether. Unfortuantely, I'm having a difficult time 
> moving over to Nginx rewrite rules. I'll post my attempt at the Nginx 
> rewrite rules, but first I'm going to post the mod_rewrite rules of my 
> Symphony CMS site, so we have the direct comparison. If anyone knows 
> Nginx... your help is much appreciated!!!
> 
> Here's the regular mod_rewrite for the index.php in document root folder 
> for Symphony...
> 
> Original Symphony 2 .htaccess
> 
> ### DO NOT APPLY RULES WHEN REQUESTING "favicon.ico"
> RewriteCond %{REQUEST_FILENAME} favicon.ico [NC]
> RewriteRule .* - [S=14]
> 
> ### IMAGE RULES
> RewriteRule 
> ^image\/3\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-f0-9]{3,6})(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8 [L]
> RewriteRule 
> ^image\/2\/([0-9]+)\/([0-9]+)\/([1-9])(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7 [L]
> RewriteRule 
> ^image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6 [L]
> RewriteRule ^image(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=0:0:0:0:0:$2:$3.$4 [L]
> 
> ### CHECK FOR TRAILING SLASH - Will ignore files
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_URI} !/$
> RewriteCond %{REQUEST_URI} !(.*)/$
> RewriteRule ^(.*)$ /$1/ [L,R=301]
> 
> ### MAIN REWRITE - This will ignore directories
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)\/$ /index.php?page=$1&%{QUERY_STRING} [L]

 server {

     root ...

     location = /favicon.ico {
     }

     location / {
         if (!-d $request_filename) {
             rewrite ^/(.+)$ /index.php?url=$1 last;
     }

     location ^~ /image/2/ {
         rewrite  ^/image/2/(\d+)/(\d+)/(\d)(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$
                  /symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7;
     }

     location ^~ /image/3/ {
         rewrite
       "^/image/3/(\d+)/(\d+)/(\d)/(\w{3,6})(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$"
            /symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8;
     }

     location ~ ^/image/ {

         rewrite  ^/image/1/(\d+)\/(\d+)(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$
                  /symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6
                  last;

         rewrite  ^/image(/[01])?/(.+)\.(gif|jpe?g|png|bmp)$ 
                  /symphony/image.php?param=0:0:0:0:0:$2:$3.$4;
     }

     location ~ \.php$ {
         proxy_pass  ...
     }
}

However, I do not understand what this part is intended for:

> ### CHECK FOR TRAILING SLASH - Will ignore files
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_URI} !/$
> RewriteCond %{REQUEST_URI} !(.*)/$
> RewriteRule ^(.*)$ /$1/ [L,R=301]
> 
> ### MAIN REWRITE - This will ignore directories
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteRule ^(.*)\/$ /index.php?page=$1&%{QUERY_STRING} [L]

Probably this may be served something else better than ugly:

     location / {
         if (!-d $request_filename) {
             rewrite ^/(.+)$ /index.php?url=$1 last;
     }


> My Nginx attempt
> 
> I don't think this is right, but would love to get some input.
> 
> location / {
>     if ($request_filename="favicon.ico") {
>         rewrite  ^(.*)$  break;
>     }
> 
>     ### IMAGE RULES
>     rewrite 
> ^image\/3\/([0-9]+)\/([0-9]+)\/([1-9])\/([a-fA-f0-9]{3,6})(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=3:$1:$2:$3:$4:$6:$7.$8 last;
>     rewrite 
> ^image\/2\/([0-9]+)\/([0-9]+)\/([1-9])(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=2:$1:$2:$3:0:$5:$6.$7 last;
>     rewrite 
> ^image\/1\/([0-9]+)\/([0-9]+)(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=1:$1:$2:0:0:$4:$5.$6 last;
>     rewrite ^image(\/(0|1))?\/(.+)\.(jpg|gif|jpeg|png|bmp)$ 
> /symphony/image.php?param=0:0:0:0:0:$2:$3.$4 last;
> 
>     ### CHECK FOR TRAILING SLASH - Will ignore files
>     if (!-f $request_filename) {
>         rewrite ^/(.+)$ /$1/ permanent;
>     }
> 
>     ### MAIN REWRITE - This will ignore directories
>     if (!-d $request_filename) {
>         rewrite ^/(.+)$ /index.php?url=$1 last;
>     }
> }
> -- 
> Posted via http://www.ruby-forum.com/.

-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list