What is trailing slash normalization in web servers?
Trailing slash normalization means making sure URLs either always end with a slash or never do, to keep URLs consistent and avoid duplicate content.
Click to reveal answer
beginner
Why normalize trailing slashes in URLs?
To improve SEO by avoiding duplicate content and to ensure consistent URL handling by the server and browsers.
Click to reveal answer
intermediate
How do you redirect URLs without trailing slash to ones with trailing slash in nginx?
Use a rewrite rule like:
rewrite ^([^.]*[^/])$ $1/ permanent;
This adds a slash at the end if missing.
Click to reveal answer
intermediate
What does the nginx directive try_files $uri $uri/ =404; do related to trailing slashes?
It tries to serve the exact URI first, then the URI with a trailing slash, and returns 404 if neither exists. Helps handle directories and files properly.
Click to reveal answer
intermediate
What is the difference between a 301 and 302 redirect in trailing slash normalization?
301 is a permanent redirect telling browsers and search engines the URL changed permanently. 302 is temporary. Use 301 for trailing slash normalization to keep SEO benefits.
Click to reveal answer
Which nginx directive is commonly used to add a trailing slash to URLs missing it?
Aroot /var/www/html;
Bproxy_pass http://backend;
Clisten 80;
Drewrite ^([^.]*[^/])$ $1/ permanent;
✗ Incorrect
The rewrite directive with that regex adds a trailing slash permanently.
What status code should you use for permanent trailing slash redirects?
A200
B301
C302
D404
✗ Incorrect
301 means permanent redirect, which is best for SEO and trailing slash normalization.
What does the following nginx config do?
try_files $uri $uri/ =404;
ABlocks all requests
BAlways redirects to homepage
CTries file, then directory, else 404
DEnables gzip compression
✗ Incorrect
It tries to serve the exact file, then the directory with slash, else returns 404.
Why avoid having both /page and /page/ accessible on your site?
AIt causes duplicate content issues for SEO
BIt confuses users
CIt slows down the server
DIt breaks HTTPS
✗ Incorrect
Duplicate content can hurt SEO rankings, so normalize URLs to one form.
Which is true about trailing slash normalization?
AIt ensures consistent URL format
BIt only applies to image files
CIt disables caching
DIt requires changing DNS settings
✗ Incorrect
Normalization makes URLs consistent, improving user experience and SEO.
Explain how to configure nginx to redirect URLs without trailing slashes to ones with trailing slashes.
Think about using rewrite with a regex and permanent flag.
You got /4 concepts.
Describe why trailing slash normalization is important for websites and SEO.
Consider how search engines treat URLs with and without slashes.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of trailing slash normalization in nginx?
easy
A. To disable URL rewriting completely
B. To block all URLs that end with a slash
C. To serve different content based on trailing slash presence
D. To ensure URLs are consistent by adding or removing trailing slashes
Solution
Step 1: Understand URL consistency
Trailing slash normalization makes URLs uniform by either adding or removing the slash at the end.
Step 2: Identify nginx role
nginx uses redirects to enforce this consistency, improving user experience and SEO.
Final Answer:
To ensure URLs are consistent by adding or removing trailing slashes -> Option D
A. Using $request_uri instead of $uri causes double query strings
B. Missing semicolon after return statement
C. The regex should check for trailing slash presence, not absence
D. Using if inside location is not allowed
Solution
Step 1: Understand variables used
$request_uri includes query string, $uri does not. Adding slash to $uri is safer.
Step 2: Identify problem with $request_uri
Appending slash to $request_uri can cause double query strings or malformed URLs.
Final Answer:
Using $request_uri instead of $uri causes double query strings -> Option A
Quick Check:
Use $uri for clean URL redirects [OK]
Hint: Use $uri, not $request_uri, to avoid query duplication [OK]
Common Mistakes:
Thinking if inside location is forbidden
Ignoring query string issues
Misreading regex logic
5. How would you configure nginx to always add a trailing slash to URLs except for requests to files (like .html or .jpg)?
hard
A. Use proxy_pass to add trailing slash
B. Always append slash to all URLs unconditionally
C. Use an if condition to check if URI lacks slash and does not end with a file extension, then return 301 with slash
D. Disable trailing slash normalization for all URLs
Solution
Step 1: Define condition for adding slash
Check if URL does not end with slash and does not match file extensions like .html, .jpg.
Step 2: Use if and return 301 for redirect
Use nginx if directive with regex to exclude files, then redirect to URL with trailing slash.
Final Answer:
Use an if condition to check if URI lacks slash and does not end with a file extension, then return 301 with slash -> Option C
Quick Check:
Conditional redirect excluding files = Use an if condition to check if URI lacks slash and does not end with a file extension, then return 301 with slash [OK]
Hint: Exclude files by regex, redirect others with slash [OK]
Common Mistakes:
Appending slash to all URLs including files
Using proxy_pass for redirects
Disabling normalization entirely
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.