【问题标题】:Re-work Apache .htaccess to nginx configuration将 Apache .htaccess 重新工作到 nginx 配置
【发布时间】:2016-05-30 22:29:50
【问题描述】:

我正在尝试重写我的 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]

【问题讨论】:

  • 我可以使用凹凸吗?

标签: .htaccess nginx


【解决方案1】:

我假设您具有以下形式的 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 需要在另一个位置进行处理。详情请见this document

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2016-01-16
    • 2014-04-24
    • 2021-10-21
    相关资源
    最近更新 更多