Please help convert htaccess to nginx

Discussion in 'HOWTO-Related Questions' started by ababneh, Mar 25, 2013.

  1. ababneh

    ababneh New Member

    Hey,

    I installed ispconfig/nginx on ubuntu 12.10 following your great how-to:

    http://www.howtoforge.com/perfect-server-ubuntu-12.10-nginx-bind-dovecot-ispconfig-3-p4

    I need to apply rewrite rules to nginx from a .haccess file with these contents:

    Code:
    RewriteEngine On
    RewriteRule ^index.html$ index.php
    RewriteRule ^browse-(.*)-videos.html$ category.php?cat=$1
    RewriteRule ^browse-(.*)-videos-([0-9]+)-(.*).html$ category.php?cat=$1&page=$2&sortby=$3
    
    RewriteRule ^register.html$ register.php
    RewriteRule ^contact_us.html$ contact_us.php
    RewriteRule ^edit_profile.html$ edit_profile.php
    RewriteRule ^suggest.html$ suggest.php
    RewriteRule ^upload.html$ upload.php
    RewriteRule ^upload_avatar.html$ upload_avatar.php
    RewriteRule ^suggest.html$ suggest.php
    
    RewriteRule ^favorites.html(.*)$ favorites.php$1
    RewriteRule ^login.html(.*)$ login.php$1
    RewriteRule ^newvideos.html(.*)$ newvideos.php$1
    RewriteRule ^topvideos.html(.*)$ topvideos.php$1
    RewriteRule ^profile.html(.*)$ profile.php$1
    RewriteRule ^favorites.html(.*)$ favorites.php$1
    RewriteRule ^playlist/(.*)$ myfavorites.php?u=$1
    RewriteRule ^memberlist.html(.*)$ memberlist.php$1
    
    RewriteRule ^articles/read-(.*)_([0-9]+).html$ article_read.php?a=$2
    RewriteRule ^articles/index-([0-9]+).html$ article.php?page=$1
    RewriteRule ^articles/browse-(.*)-([0-9]+).html$ article.php?c=$1&page=$2
    RewriteRule ^articles/tag/([^/]+)/page-([0-9]+)(/)?$ article.php?tag=$1&page=$2
    RewriteRule ^articles/tag/([^/]+)(/)?$ article.php?tag=$1&page=1
    RewriteRule ^articles/popular-([0-9]+).html$ article.php?show=popular&page=$1
    RewriteRule ^articles(\/|.html)?$ article.php
    RewriteRule ^article(\/|.html)?$ article.php
    RewriteRule ^pages/(.*)\.html$ page.php?name=$1
    
    RewriteRule ^tags/([^/]+)/$ tag.php?t=$1&page=1
    RewriteRule ^tags/([^/]+)/page-([0-9]+)(/)?$ tag.php?t=$1&page=$2
    RewriteRule ^(.*)/(.*)-video_(.*).html$ musicvideo.php?vid=$3
    RewriteRule ^([^/]*)-video_(.*).html$ watch.php?vid=$2
    RewriteRule ^fpembed-(.*).swf$ fpembed.php?vid=$1
    RewriteRule ^rss.xml$ rss.php [L]
    I am not an expert in these things so it is all try and error. I used this online converter http://winginx.com/htaccess and I got this output:

    Code:
    # nginx configuration
    
    location = /index.html {
      rewrite ^(.*)$ /index.php;
    }
    
    location /browse {
      rewrite ^/browse-(.*)-videos.html$ /category.php?cat=$1;
      rewrite ^/browse-(.*)-videos-([0-9]+)-(.*).html$ /category.php?cat=$1&page=$2&sortby=$3;
    }
    
    location = /register.html {
      rewrite ^(.*)$ /register.php;
    }
    
    location = /contact_us.html {
      rewrite ^(.*)$ /contact_us.php;
    }
    
    location = /edit_profile.html {
      rewrite ^(.*)$ /edit_profile.php;
    }
    
    location = /suggest.html {
      rewrite ^(.*)$ /suggest.php;
      rewrite ^(.*)$ /suggest.php;
    }
    
    location = /upload.html {
      rewrite ^(.*)$ /upload.php;
    }
    
    location = /upload_avatar.html {
      rewrite ^(.*)$ /upload_avatar.php;
    }
    
    location /favorites {
      rewrite ^/favorites.html(.*)$ /favorites.php$1;
      rewrite ^/favorites.html(.*)$ /favorites.php$1;
    }
    
    location /login {
      rewrite ^/login.html(.*)$ /login.php$1;
    }
    
    location /newvideos {
      rewrite ^/newvideos.html(.*)$ /newvideos.php$1;
    }
    
    location /topvideos {
      rewrite ^/topvideos.html(.*)$ /topvideos.php$1;
    }
    
    location /profile {
      rewrite ^/profile.html(.*)$ /profile.php$1;
    }
    
    location /playlist {
      rewrite ^/playlist/(.*)$ /myfavorites.php?u=$1;
    }
    
    location /memberlist {
      rewrite ^/memberlist.html(.*)$ /memberlist.php$1;
    }
    
    location /articles {
      rewrite ^/articles/read-(.*)_([0-9]+).html$ /article_read.php?a=$2;
      rewrite ^/articles/index-([0-9]+).html$ /article.php?page=$1;
      rewrite ^/articles/browse-(.*)-([0-9]+).html$ /article.php?c=$1&page=$2;
      rewrite ^/articles/tag/([^/]+)/page-([0-9]+)(/)?$ /article.php?tag=$1&page=$2;
      rewrite ^/articles/tag/([^/]+)(/)?$ /article.php?tag=$1&page=1;
      rewrite ^/articles/popular-([0-9]+).html$ /article.php?show=popular&page=$1;
      rewrite ^/articles(\/|.html)?$ /article.php;
    }
    
    location /article {
      rewrite ^/article(\/|.html)?$ /article.php;
    }
    
    location /pages {
      rewrite ^/pages/(.*)\.html$ /page.php?name=$1;
    }
    
    location /tags {
      rewrite ^/tags/([^/]+)/$ /tag.php?t=$1&page=1;
      rewrite ^/tags/([^/]+)/page-([0-9]+)(/)?$ /tag.php?t=$1&page=$2;
    }
    
    location / {
      rewrite ^/(.*)/(.*)-video_(.*).html$ /musicvideo.php?vid=$3;
      rewrite ^/([^/]*)-video_(.*).html$ /watch.php?vid=$2;
    }
    
    location /fpembed {
      rewrite ^/fpembed-(.*).swf$ /fpembed.php?vid=$1;
    }
    
    location = /rss.xml {
      rewrite ^(.*)$ /rss.php break;
    }
    So then from SSH I typed:
    nano /etc/nginx/nginx.conf
    and pasted the above content under http {
    then ran:
    /etc/init.d/nginx start
    and here is the error message:
    Code:
    Starting nginx: nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:75
    nginx: configuration file /etc/nginx/nginx.conf test failed
    
    Ok so I used another converter located here http://www.anilcetin.com/convert-apache-htaccess-to-nginx/
    its output is:
    Code:
    	rewrite ^/index.html$ /index.php;
    	rewrite ^/browse-(.*)-videos.html$ /category.php?cat=$1;
    	rewrite ^/browse-(.*)-videos-([0-9]+)-(.*).html$ /category.php?cat=$1&page=$2&sortby=$3;
    	rewrite ^/register.html$ /register.php;
    	rewrite ^/contact_us.html$ /contact_us.php;
    	rewrite ^/edit_profile.html$ /edit_profile.php;
    	rewrite ^/suggest.html$ /suggest.php;
    	rewrite ^/upload.html$ /upload.php;
    	rewrite ^/upload_avatar.html$ /upload_avatar.php;
    	rewrite ^/suggest.html$ /suggest.php;
    	rewrite ^/favorites.html(.*)$ /favorites.php$1;
    	rewrite ^/login.html(.*)$ /login.php$1;
    	rewrite ^/newvideos.html(.*)$ /newvideos.php$1;
    	rewrite ^/topvideos.html(.*)$ /topvideos.php$1;
    	rewrite ^/profile.html(.*)$ /profile.php$1;
    	rewrite ^/favorites.html(.*)$ /favorites.php$1;
    	rewrite ^/playlist/(.*)$ /myfavorites.php?u=$1;
    	rewrite ^/memberlist.html(.*)$ /memberlist.php$1;
    	rewrite ^/articles/read-(.*)_([0-9]+).html$ /article_read.php?a=$2;
    	rewrite ^/articles/index-([0-9]+).html$ /article.php?page=$1;
    	rewrite ^/articles/browse-(.*)-([0-9]+).html$ /article.php?c=$1&page=$2;
    	rewrite ^/articles/tag/([^/]+)/page-([0-9]+)(/)?$ /article.php?tag=$1&page=$2;
    	rewrite ^/articles/tag/([^/]+)(/)?$ /article.php?tag=$1&page=1;
    	rewrite ^/articles/popular-([0-9]+).html$ /article.php?show=popular&page=$1;
    	rewrite ^/articles(/|.html)?$ /article.php;
    	rewrite ^/article(/|.html)?$ /article.php;
    	rewrite ^/pages/(.*).html$ /page.php?name=$1;
    	rewrite ^/tags/([^/]+)/$ /tag.php?t=$1&page=1;
    	rewrite ^/tags/([^/]+)/page-([0-9]+)(/)?$ /tag.php?t=$1&page=$2;
    	rewrite ^/(.*)/(.*)-video_(.*).html$ /musicvideo.php?vid=$3;
    	rewrite ^/([^/]*)-video_(.*).html$ /watch.php?vid=$2;
    	rewrite ^/fpembed-(.*).swf$ /fpembed.php?vid=$1;
    	rewrite ^/rss.xml$ /rss.php last;
    and when I copy this code into nginx conf and start nginx here is what I get:
    Code:
    Starting nginx: nginx: [emerg] "rewrite" directive is not allowed here in /etc/nginx/nginx.conf:73
    nginx: configuration file /etc/nginx/nginx.conf test failed
    
    I am lost! Please anyone tell me what I am doing wrong!!

    Thank you for your time..
     
  2. falko

    falko Super Moderator Howtoforge Staff

    You probably copied the rewrite rules to the wrong place. Please post your full vhost configuration (including the rewrite rules from anilcetin).
     
  3. ababneh

    ababneh New Member

    where would that be located?? thank you
     
  4. ababneh

    ababneh New Member

    Hello. Could someone help out even if for $ just let me know.. it is urgent !
     
  5. ababneh

    ababneh New Member

    grrr why no one said anything about nginx directives in ispconfig. it worked from there.
     
  6. falko

    falko Super Moderator Howtoforge Staff

    You didn't mention that you're using ISPConfig.
     
  7. ababneh

    ababneh New Member

Share This Page