Join the community today
Become a Member

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

    25
    10
    3
    Sep 15, 2014
    Ratings:
    +10
    Local Time:
    11:43 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

    55,189
    12,251
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,829
    Local Time:
    4:43 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    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