Thread: NGINX Rewrite

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    9
    Rep Power
    0

    Smile NGINX Rewrite


    Hey everyone

    New to Devshed and this is my first post so I'll get straight to it.

    I have created a mini MVC framework using .htaccess to route the urls which worked great on my Mamp Apache server.

    The problem is my live server is nginx. I see that regular MAMP has an nginx serve which I am now ussing but every page but the home page 404's

    Can anyone help me wrap my head around this nginx stuff and get it woking with MAMP?

    I'v done research into how to write rewrite rules for nginx and the benefits off nginx handling large traffic which I will need very soon connections, but I can't seem to get the 404's to stop. Thanks in advance guys.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    9
    Rep Power
    0
    Here is my main app.php

    PHP Code:
    <?php

    class App {

        protected 
    $controller 'home';
        protected 
    $method 'index';
        protected 
    $params = [];

        public function 
    __construct()
        {
            
    $url $this->parseUrl();

            if (
    file_exists('application/controllers/' $url[0] . '.php'))
            {
                
    $this->controller $url[0];
                unset(
    $url[0]);
            }

            require_once 
    'application/controllers/' ucwords($this->controller) . '.php';

            
    $this->controller = new $this->controller;

            if (isset(
    $url[1]))
            {
                if (
    method_exists($this->controller$url[1]))
                {
                    
    $this->method $url[1];
                    unset(
    $url[1]);
                }
            }

            
    $this->params $url array_values($url) : [];

            
    call_user_func_array([$this->controller$this->method], $this->params);
        }

        public function 
    parseUrl()
        {
            if (isset(
    $_GET['url']))
            {
                return 
    $url explode('/'filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
            }
        }
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    9
    Rep Power
    0
    plus my .htaccess

    Code:
    Options -MultiViews
    
    RewriteEngine On
    
    RewriteBase /
    
    
    # Offers -> VSL
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^free-video-([0-9]+)$ offers/vsl/free-video-$1 [NC,L]
    
    
    # Offers -> Order
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^order/offer-([0-9]+)$ offers/order/offer-$1 [NC,L]  
    
    
    # Offers -> Tetter
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^letter$ offers/letter/letter [NC,L]  
    
    
    # Offers -> Trial
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^trial$ offers/trial/trial [NC,L]  
    
    
    # Optins -> ecourse
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^ecourse$ optins/ecourse [NC,L]  
    
    
    # Funnels -> upsale_1
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-u1-nr$ funnels/upsale_1 [NC,L] 
    
    
    # Funnels -> upsale_2
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-u2-nr$ funnels/upsale_2 [NC,L]
    
    
    # Funnels -> downsale_1
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-d1-nr$ funnels/downsale_1 [NC,L]
    
    
    # Funnels -> downsale_2
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-d2-nr$ funnels/downsale_2 [NC,L] 
    
    # OLD FUNNEL
    # Funnels -> old_upsale_1
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-u1.php$ funnels/old_upsale_1 [NC,L] 
    
    
    # Funnels -> old_upsale_2
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-u2.php$ funnels/old_upsale_2 [NC,L]
    
    
    # Funnels -> old_downsale_1
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-d1.php$ funnels/old_downsale_1 [NC,L]
    
    
    # Funnels -> old_downsale_2
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-d2.php$ funnels/old_downsale_2 [NC,L] 
    
    
    # Funnels -> old_upsale_2
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^add-to-order-u3.php$ funnels/old_upsale_3 [NC,L] 
    
    
    # Funnels -> thankyou
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^thankyou$ funnels/thankyou [NC,L] 
    
    
    # TOS
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^terms-and-conditions$ tos [NC,L]  
    
    
     index
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
    
    
    ErrorDocument 404 /application/controllers/home.php
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    nginx doesn't do .htaccess files. You have to write configuration specifically for it that does the URL rewriting you need. There's no way around that.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    9
    Rep Power
    0
    Thanks for responding I assume your talking about nginx.conf right? The problem is I can't seem to find out where to put the file, at least not relative to MAMP so I can do some testing.

    I cant really find any good tutorials for writing nginx with MAMP either. Do you know any?
  10. #6
  11. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    "writing nginx with MAMP" does not make sense.

    Apache and nginx are both web servers. You cannot use both. It's okay if you use MAMP for development and then nginx for your live site, but your code has to support both: put your URL rewriting in .htaccess for MAMP, then set up the equivalent URL rewriting configuration for nginx. You should also make sure nginx responds with a 404 if someone tries to access the .htaccess file (which is something Apache does for you automatically).

    In other words, stop editing your code and .htaccess files and start editing your nginx configuration to match it.
    * Module ngx_http_rewrite_module
    * https://www.nginx.com/blog/creating-...rewrite-rules/
    * https://www.nginx.com/blog/convertin...rewrite-rules/
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    9
    Rep Power
    0
    Sorry I wasn't very clear about my MAMP question. There is an option to set MAMP to nginx. Do u know where I should create the configuration file?

  14. #8
  15. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    But... MAMP using nginx would be MNMP!

    I don't know. I suspect MAMP doesn't support it. See, nginx is not like Apache where you load a bunch of modules and stuff: you define a configuration and then build nginx. All your stuff goes into the web server itself directly without needing .htaccess files or mod_anything - and that's actually nginx's biggest selling point.

    So I don't know if simply switching MAMP to nginx will suffice here. I certainly haven't seen any documentation about configuring nginx with MAMP...
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2016
    Posts
    9
    Rep Power
    0
    ah thats too bad. thanks for all your help anyway

    Does this site have like a repping system so i can +rep you for your help?

    Comments on this post

    • SimonJM agrees : Rep given on your behalf to Requnix ^_^
  18. #10
  19. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,279
    Rep Power
    9645
    Meh, you can (maybe, I'm not sure with new user accounts) but it's not really that important.

    Comments on this post

    • SimonJM agrees : Rep by proxy, for jaywilson :)
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    936
    Rep Power
    279
    According to the "revision history" on the MAMP website, the MAMP package does include the nginx web server. It runs on port 7888 by default, as opposed to the Apache Web Server , which runs on port 8888 by default.
    I downloaded and extracted the MAC version and found a conf directory that contains the nginx .conf files.

    Use Finder and look in Applications/MAMP for a conf sub-folder, that is where you would create/edit the nginx conf files to setup the rewrite rules.

    Comments on this post

    • requinix agrees
    Last edited by DonR; July 10th, 2016 at 08:17 PM.

IMN logo majestic logo threadwatch logo seochat tools logo