简体   繁体   中英

nginx rewrite rule not working?

rewrite ^/index\.asp /index.php last;
rewrite ^/index\.asp\?boardid=([0-9]+)$ /forum-$1-1.html last;
rewrite ^/index\.asp\?boardid=([0-9]+)(.*)$ /forum-$1-1.html last;
rewrite ^/index_([0-9]+)(.*)$ /forum-$1-1.html last;
rewrite ^/dispbbs\.asp\?boardID=([0-9]+)&ID=([0-9]+)$ /thread-$2-1-1.html last;

I have try out rewrite rules above, and get a dead result, not working. I have refer to many posts and articles, and no help.

Is there any mistakes?

V/R, gavin


Thanks for your reply. :)

I have altered my nginx config to,

rewrite ^/index\.asp$ /index.php last;
rewrite ^/index\.asp\?boardid=([0-9]+)(.*)$ /forum-$1-1.html last;
rewrite ^/index\.asp\?boardid=([0-9]+)$ /forum-$1-1.html last;
rewrite ^/dispbbs\.asp\?boardID=([0-9]+)&ID=([0-9]+)$ /thread-$2-1-1.html last;

Still not working. But I find no mistake in the rules.

You cannot match arguments in rewrite rules, they may include paths only. The reasons are simple: suppose arguments may have another order; suppose there can be additional arguments you did not take into account (eg keywords from Google).

So your rules should be rewritten in a way to match path at the first and then check arguments. Like this:

rewrite ^/index_([0-9]+)(.*)$ /forum-$1-1.html last;

location /index.asp {
  if ($arg_boardid ~ "^([0-9]+)") {
    rewrite ^ /forum-$1-1.html break;
  }
  rewrite ^ /index.php break;
}

location /dispbbs.asp {
  rewrite ^ /thread-$arg_ID-1-1.html break;
}

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