简体   繁体   中英

Translating Rules from Apache to Nginx

I have switched server from Apache to Nginx.

Everything works, except for the last bit.

In Apache I have the following rules:

RewriteRule ^event/(.*)$ /event.html [L]
RewriteRule ^/event/(.*)$ /event.html [L]
RewriteRule ^venue/(.*)$ /venue.html [L]
RewriteRule ^/venue/(.*)$ /venue.html [L]
RewriteRule ^calendar/day/(.*)$ /calendar/day.html [L]
RewriteRule ^/calendar/day/(.*)$ /calendar/day.html [L]
RewriteRule ^calendar/month/(.*)$ /calendar/month.html [L]
RewriteRule ^/calendar/month/(.*)$ /calendar/month.html [L]

But can't get them right in Nginx. The following is the implementation:

location /event {
  rewrite ^/event/(.*)$ /event.html break;
  rewrite ^/event/(.*)$ /event.html break;
}

location /venue {
  rewrite ^/venue/(.*)$ /venue.html break;
  rewrite ^/venue/(.*)$ /venue.html break;
}

location /calendar {
  rewrite ^/calendar/day/(.*)$ /calendar/day.html break;
  rewrite ^/calendar/day/(.*)$ /calendar/day.html break;
  rewrite ^/calendar/month/(.*)$ /calendar/month.html break;
  rewrite ^/calendar/month/(.*)$ /calendar/month.html break;
}

But I get redirected to the Home Page when the above are accessed.

Can someone help me please?

Seems like the path after the slash is duplicated due to already been defined via location . Try simply this:

location / {
    rewrite ^/event/(.*)$ /event.html last;
    rewrite ^/venue/(.*)$ /venue.html last;
    rewrite ^/calendar/day/(.*)$ /calendar/day.html last;
    rewrite ^/calendar/month/(.*)$ /calendar/month.html 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