【发布时间】:2018-09-12 01:46:29
【问题描述】:
我正在从 Apache (htaccess) 切换到 Nginx,但我不知道如何让重写工作。
每个文件夹都有一个.htaccess 和一个index.php:
test1/
- .htaccess
- index.php
test2/
- .htaccess
- index.php
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
index.php
switch ($_GET['rt']) {
default:
echo 'default page';
break;
case 'page2':
echo 'page2';
break;
}
如果用户访问 URL example.com/test1,它会显示“默认页面”(实际 URL 将是 example.com/test1/index.php)。
或者如果他们转到example.com/test2/page2(实际网址为example.com/test2/index.php?rt=page2),页面将显示“page2”。
我认为可以工作的 Nginx 块:
location /test1 {
try_files $uri $uri/ index.php?rt=$uri;
}
【问题讨论】: