简体   繁体   English

htaccess重写为Nginx错误

[英]htaccess rewrite to nginx bugs

i had this code in my old .htaccess file 我在旧的.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]

and I've recently upgraded to nginx and I got the following code to use in my config instead of the Apache rewrite rules. 并且我最近已升级到nginx,并且在我的配置中使用了以下代码,而不是Apache重写规则。

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

This works fine too, but when I try and load my stylesheet in /Style/Main.css, it tries loading the file as a get request. 这也可以正常工作,但是当我尝试在/Style/Main.css中加载样式表时,它会尝试将文件作为get请求加载。 How can I fix this so it doesn't do this? 如何解决此问题,使其无法执行此操作?

You need a try_files to check whether the requested url (in your case, it would be css, js, etc.) exists or not, before you pass it to the rewrite. 在将请求的URL传递给重写之前,您需要一个try_files来检查所请求的URL(在您的情况下为css,js等)是否存在。 Try something like: 尝试类似:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM