Search code examples
nginxurl-rewritingfriendly-url

Can Nginx substring the $request_uri?


I want to substring $request_uri
For example, The URL address is:https://example/surveyssl/survey
If I using $request_uri, it will get "surveyssl/survey"
Can I substring "surveyssl" so that I can get "survey" only?

location /surveyssl/survey{
             proxy_pass http://ssldev/surveyssl/index.php/$request_uri;
             #It will fail because the url output is 
             #http://ssldev/surveyssl/index.php/surveyssl/survey
             #but I want to get this url:
             #http://ssldev/surveyssl/index.php/survey
 }

Solution

  • With your shown sample, could you please try following. Please make sure to clear your browser cache before testing your URLs. First ruleset is very specific one to catch only surveyssl; 2nd rules set it dynamic one where it will catch anything coming after surveyssl.

    location ~ /surveyssl/(survey/?) {
                 proxy_pass http://ssldev/surveyssl/index.php/$1;
    
     }
    

    EDIT: To make it dynamic try following rules:

    location ~ /surveyssl/([\w-]+/?) {
                 proxy_pass http://ssldev/surveyssl/index.php/$1;
    
     }