Re-work Apache .htaccess to nginx configuration



我正在尝试重写我的nginx以使用http://domain.com/main而不是使用http://domain.com/?page=main

我做过:

try_files $uri $uri/ /index.php?page=$uri;

返回空白页

rewrite ^/(w+)$ /index.php?page=$1 break;
rewrite ^/(w+)+/$ /index.php?page=$1 break;
if ($http_host !~ "^$"){
   rewrite ^(.*)$ http%1://www.$http_host$request_uri redirect;
}

生成网址:http://domain.com/main/http://domain.com/main/http://domain.com/main

这是Apache .htaccess:

RewriteEngine on
RewriteRule ^(w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(w+)+/$ index.php?page=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我假设您有一个形式的 URI 的工作配置:http://domain.com/?page=main

try_files $uri $uri/ /index.php?page=$uri;可能无法按预期工作,因为它将生成:

http://domain.com/?page=/main

请注意额外的/。因此,您可能需要使用重写来提取不包括前导/的部分 URI。例如:

location / {
    try_files $uri $uri/ @rewrite;
}
location @rewrite {
    rewrite ^/(.*)$ /index.php?page=$1 last;
}
location .php$ { ... }

请注意last后缀而不是break后缀,因为目标 URI 需要在另一个位置处理。有关详细信息,请参阅此文档。

最新更新