将 Apache 的 mod_rewrite
规则转换为 NGINX 的重写规则需要一些手动的步骤,因为两者的配置语法和处理方式有所不同。以下是一些常见的 mod_rewrite
规则及其在 NGINX 中的等价配置示例。
mod_rewrite
规则RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]
server {
listen 80;
server_name example.com;
location / {
rewrite ^/old-page$ /new-page permanent;
}
}
mod_rewrite
规则RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^page$ /new-page?id=%1 [R=301,L]
server {
listen 80;
server_name example.com;
location /page {
if ($arg_id ~ ^([0-9]+)$) {
return 301 /new-page?id=$arg_id;
}
}
}
mod_rewrite
规则RewriteEngine On
RewriteRule ^product/([0-9]+)$ /product.php?id=$1 [L]
server {
listen 80;
server_name example.com;
location /product {
rewrite ^/product/([0-9]+)$ /product.php?id=$1 last;
}
}
mod_rewrite
规则RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
mod_rewrite
规则RewriteEngine On
RewriteRule ^$ /home [L]
server {
listen 80;
server_name example.com;
location = / {
rewrite ^ /home last;
}
}
mod_rewrite
规则RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^Mozilla
RewriteRule ^old-page$ /new-page [R=301,L]
server {
listen 80;
server_name example.com;
location /old-page {
if ($http_user_agent ~* ^Mozilla) {
return 301 /new-page;
}
}
}
领取专属 10元无门槛券
手把手带您无忧上云