Nginx offers a robust configuration system that allows you to rewrite URLs using various rules. URL rewriting is a crucial aspect of web server configuration, enabling you to enhance user experience, improve SEO, and ensure better control over your website’s structure. In this article, we’ll explore how to rewrite URLs in Nginx using different rules with practical command examples.
Simple Redirect
To perform a basic redirect, you can use the rewrite
directive. For instance, redirecting all traffic from HTTP to HTTPS:
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
Removing Trailing Slashes
Removing trailing slashes from URLs can be achieved as follows:
rewrite ^/(.*)/$ /$1 permanent;
This rule ensures that URLs ending with a slash get redirected to the same URL without the trailing slash.
Query String Manipulation
Nginx allows you to manipulate query strings easily. For instance, rewriting example.com/page?user=john
to example.com/user/john
:
location /page {
if ($args ~* "^user=(.*)$") {
rewrite ^/page$ /user/$1? permanent;
}
}
Clean and SEO-Friendly URLs
Creating clean URLs is essential for SEO. Consider transforming example.com/product?id=123
into example.com/product/123
:
location /product {
rewrite ^/product\?id=(.*)$ /product/$1 permanent;
}
Rewriting Based on File Existence
You can rewrite URLs based on the existence of a file. For example, redirecting non-existent files to a front controller:
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?$1 last;
}
Rewriting Based on User Agent
Customize URL rewriting based on user-agent, useful for mobile redirection:
if ($http_user_agent ~* (iPhone|Android)) {
rewrite ^/$ /mobile/index.html last;
}
Extracting Parameters from URLs
Regular expressions can be employed to extract parameters from URLs. For instance, converting example.com/user?id=123&name=john
to example.com/user/123/john
:
location ~ ^/user {
rewrite ^/user\?id=(\d+)&name=(\w+)$ /user/$1/$2 permanent;
}
This regex-based rule captures numerical and alphabetical parameters and rewrites the URL accordingly.
Removing File Extensions
To create cleaner and extension-less URLs, you can remove file extensions like .html
:
location / {
rewrite ^(/.*\.html)$ $scheme://$host$1 permanent;
}
This rule removes the .html
extension from URLs, providing a more aesthetic and SEO-friendly structure.
Redirecting Old URLs to New Ones
When restructuring your website, redirecting old URLs to new ones is crucial for maintaining SEO and preserving user bookmarks. Use the location
block for specific paths:
location /old-path {
rewrite ^/old-path(.*)$ /new-path$1 permanent;
}
This rule redirects all traffic from /old-path
to the corresponding location in the new structure.
Case-Insensitive Redirects
Ensure case-insensitive redirects for URLs:
location ~* /case-insensitive {
rewrite ^/case-insensitive(.*)$ /new-path$1 permanent;
}
The ~*
modifier makes the matching case-insensitive, redirecting requests regardless of the letter case.
Blocking Access to Sensitive Paths
URL rewriting can be used to block access to sensitive paths. For example, denying access to /admin
:
location ~ ^/admin {
rewrite ^ /403_error.html break;
}
This rule prevents direct access to the /admin
path and redirects users to a 403 error page.
Adding Security Tokens to URLs
Enhance security by adding tokens to URLs, allowing access only with a valid token:
location /secure-path {
rewrite ^/secure-path/(.*)$ /secure-path/$1?token=your_secret_token last;
}
This ensures that access to the /secure-path
requires a valid token, providing an additional layer of security.
Where are the Nginx rewrite rules placed?
In Nginx, rewrite rules are typically placed within server blocks in the server configuration file. The server configuration file is often named nginx.conf
or divided into separate files within the /etc/nginx/conf.d/
directory.
Here’s a basic example of where rewrite rules might be placed in an Nginx configuration:
server {
listen 80;
server_name example.com;
location / {
# Your other configuration directives for handling requests
# Rewrite rules go here
rewrite ^/old-path$ /new-path permanent;
rewrite ^(/.*\.html)$ $scheme://$host$1 permanent;
}
# Additional server blocks for other domains or configurations
}
In this example:
- The
server
block defines the configuration for the specific server or virtual host.
- The
location /
block is where you handle requests for the root path, and it’s a common place to put rewrite rules..
- Inside the
location /
block, you can see two examples of rewrite rules.
It’s important to place rewrite rules in the appropriate context within the configuration file. Depending on the nature of the rewrite, you might place rules in the server block, location block, or even in a separate file included in the main configuration.
Conclusion
Nginx’s URL rewriting capabilities offer a wide range of options for tailoring your website’s URLs to meet specific requirements. From basic redirects to advanced manipulations using regular expressions and conditional rules, these examples demonstrate the flexibility and power of Nginx in URL rewriting. Experiment with these techniques to achieve a well-organized, SEO-friendly, and secure URL structure for your web application.