简体   繁体   中英

htaccess rewrite to nginx bugs

i had this code in my old .htaccess file

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.

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. 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. Try something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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