Welcome to Centmin Mod Community
Register Now

Nginx Rewrite rules help!!

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by Mike Guista, Sep 15, 2015.

  1. Mike Guista

    Mike Guista New Member

    20
    10
    3
    Sep 15, 2014
    Ratings:
    +10
    Local Time:
    2:29 AM
    1.7.6
    10.x
    Hello,
    I'm using a PHP website where I want seo friendly URL.

    Example; www.mywebsite.com/about.php to www.mywebsite.com/about-us

    So, I'm using the following rewrite rules on nginx;
    rewrite ^/about$ /about.php last;

    That gives me www.mywebsite.com/about-us link but when I open www.mywebsite.com/about.php it opens the .php extension link too. Anyway I can force .php link to redirect to seo friendly URL?

    Can anyone help me with a better rewrite rules?

    Thank you
     
  2. eva2000

    eva2000 Administrator Staff Member

    30,897
    6,907
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,402
    Local Time:
    8:29 PM
    Nginx 1.13.x
    MariaDB 5.5
    rewrites aren't my specialty so not sure how to get around the internal redirect loop you'd have with about.php redirect unless you make a copy of about.php and name it about-us.php and then use

    Code:
    location ~ ^/about-us {
            include /usr/local/nginx/conf/php.conf;
            rewrite ^(.*)$ /about-us.php last;
    }
    
    location = /about.php {
            return 302 http://$server_name/about-us;
    }
    or

    Code:
    location ~ ^/about-us {
            include /usr/local/nginx/conf/php.conf;
            try_files $uri /$uri /about-us.php;
    }
    
    location = /about.php {
            return 302 http://$server_name/about-us;
    }
     
    Last edited: Sep 15, 2015