简体   繁体   中英

Convert .htaccess to Nginx.conf Rewrite rule

I have this rule which works as expected in .htaccess with Apache RewriteRule ^jobs/([A-Za-z0-9_]+)/?([A-Za-z0-9_-]+)?$ index.php?link1=jobs&page=$1&resource_id=$2 [QSA] And it will rewrite this URL http://domain.test/jobs/createcv so that the PHP $_GET looks like this: PHP 变量转储

Now I want to achieve the same with NGINX for the same URL but it does not work.

Rule: rewrite ^/jobs/([A-Za-z0-9_]+)/?([A-Za-z0-9_-]+)$ /index.php?link1=jobs&page=$1&resource_id=$2;

But instead I have this weird result

array(3) { ["link1"]=> string(7) "jobs" ["page"]=> string(7) "createc" ["resource_id"]=> string(1) "v" }

Thank you in advance

The rewrite rule got broken during your migration from Apache to NGINX as it is missing a question mark after the second capturing group.

Add the ? in the rewrite rule and it should work:

location /jobs {
  rewrite ^/jobs/([A-Za-z0-9_]+)/?([A-Za-z0-9_-]+)?$ /index.php?link1=jobs&page=$1&resource_id=$2;
}

If the second group is optional then the v of createcv won't be "eaten" accidently like you described.

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