Home > Blockchain >  Convert .htaccess to Nginx.conf Rewrite rule
Convert .htaccess to Nginx.conf Rewrite rule

Time:05-04

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 PHP var_dump

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

CodePudding user response:

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.

  • Related