0
0
Nginxdevops~10 mins

Trailing slash normalization in Nginx - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to redirect URLs ending with a slash to the same URL without the slash.

Nginx
rewrite ^(.+)/$ [1] permanent;
Drag options to blanks, or click blank then click option'
A$1
B$1/
C$2
D$0
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1/ adds an extra slash instead of removing it.
Using $0 repeats the whole matched string including the slash.
2fill in blank
medium

Complete the code to redirect URLs without a trailing slash to the same URL with a slash.

Nginx
rewrite ^(.+[^/])$ [1] permanent;
Drag options to blanks, or click blank then click option'
A$1/
B$2/
C$1
D$0/
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 without slash does not add the trailing slash.
Using $0/ repeats the whole matched string and adds slash redundantly.
3fill in blank
hard

Fix the error in the location block to normalize trailing slashes by redirecting URLs ending with a slash to without slash.

Nginx
location / {
    if ($request_uri ~ [1]) {
        return 301 $scheme://$host$1;
    }
}
Drag options to blanks, or click blank then click option'
A^(.+)$
B^(.+)/
C^(.+)/$
D^(.+)$/
Attempts:
3 left
💡 Hint
Common Mistakes
Using a pattern without the trailing slash does not match URLs ending with slash.
Missing parentheses means $1 is undefined.
4fill in blank
hard

Fill both blanks to create a map that normalizes URLs by removing trailing slashes except for root.

Nginx
map $request_uri $normalized_uri {
    default [1];
    ~^(.+)/$ [2];
}
Drag options to blanks, or click blank then click option'
A$request_uri
B$1
C/
D$uri
Attempts:
3 left
💡 Hint
Common Mistakes
Using / as default removes all URIs incorrectly.
Using $uri instead of $request_uri changes the variable meaning.
5fill in blank
hard

Fill all three blanks to rewrite URLs to normalized form and redirect permanently.

Nginx
server {
    listen 80;
    server_name example.com;

    map $request_uri $normalized_uri {
        default [1];
        ~^(.+)/$ [2];
    }

    location / {
        if ($request_uri != [3]) {
            return 301 $scheme://$host$normalized_uri;
        }
    }
}
Drag options to blanks, or click blank then click option'
A$request_uri
B$1
C$normalized_uri
D$uri
Attempts:
3 left
💡 Hint
Common Mistakes
Using $uri instead of $request_uri in map default.
Comparing $request_uri with $uri instead of $normalized_uri.
Not capturing $1 in map regex.