nginxclean-urlspretty-urls

Pretty URL/Clean URL + nginx


I'm trying to get example.com/language/en to point to example.com/language?lang=en I've already got the @language to work (kind of, there's still some bug that I explain in the end) but unfortunately I don't know how to make the @language and the @extensionless-php to work at the same time, I tested the @language code by removing the @extensionless-php in the location / and adding the @location. And the way it is right now it accepts more than one parameter like this //example.com/language/en/en/en and I want it to return a 404 error if it has more than 1 / after the language

Current config:

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

location @language {
    rewrite ^/language/(.*)$ /language.php?lang=$1 last;
}

Solution

  • The answer was simply removing rewrite ^/language/(.*)$ /language.php?lang=$1 last; from the @language, edited the regular expression so that when the URI is /language/en/ it goes to a 404 error

    location / {
        try_files $uri $uri.html $uri/ @extensionless-php;
    }
    
    location @extensionless-php {
        rewrite ^(.*)$ $1.php last;
    }
    
    rewrite ^/language/([^/]+)$ /language.php?lang=$1 last;