我正在将现有网站从Litespeed(Apache .htaccess)迁移到Nginx,并且在转换对站点功能至关重要的特定正则表达式重写时遇到了麻烦。先前的.htaccess重写仅将先前未与特定规则匹配的/ word转换为index.php上的参数。
.htaccess优先级规则示例:
RewriteRule ^report$ index.php?page=report&%{QUERY_STRING}
RewriteRule ^([a-zA-Z0-9\-\_]+)$ index.php?page=browser®ion=$1
RewriteRule ^([a-zA-Z0-9\-\_]+)/([0-9{4}]+)/([a-zA-Z0-9\-\_]+)$ index.php?page=program®ion=$1&program=$3&year=$2&%{QUERY_STRING}
因此,如果/ report是URL,则它将加载报告页面,否则,如果它是/ arizona,则它将加载具有region = arizona的浏览器页面,依此类推。
我当前的nginx位置规则
location /report {
rewrite (?i)^/report$ /index.php?page=report&$query_string last;
}
location ^/([a-z0-9\-\_]+)$ {
rewrite (?i)^/([a-z0-9\-\_]+)$ /index.php?page=browser®ion=$1 last;
}
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
主页(URL中没有任何内容)和/ report都可以正常工作。另外,如果我手动添加/index.php?page=browser®ion=arizona,那么它也可以正常工作。
但是,输入/ arizona会导致索引下载,而不是由php-fpm处理。我对正则表达式进行了各种搜索和建议,导致其不匹配或只是下载索引。