【问题标题】:htaccess rewrite to nginx bugshtaccess 重写到 nginx 错误
【发布时间】:2015-01-02 09:39:35
【问题描述】:

我的旧 .htaccess 文件中有这段代码

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/?$ /index.php?page=$1 [L,NC]

我最近升级到了 nginx,我在配置中使用了以下代码,而不是 Apache 重写规则。

rewrite ^/(.*)$ /index.php?page=$1$args? last;

这也很好,但是当我尝试在 /Style/Main.css 中加载我的样式表时,它会尝试将文件作为获取请求加载。我怎样才能解决这个问题,让它不这样做?

【问题讨论】:

  • 有人吗?我仍然需要一个答案。

标签: apache .htaccess mod-rewrite nginx


【解决方案1】:

在将其传递给重写之前,您需要一个 try_files 来检查请求的 url(在您的情况下是 css、js 等)是否存在。尝试类似:

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

【讨论】:

    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多