0

We are moving our site from Apache to Nginx. So far, the rules that we have converted from .htaccess rules to nginx are working fine but this particular rule is giving me a hard time.

I've tried online converters which use if (Which I don't use) and manually tried to convert the rules but nothing is working. If someone can guide me in the right direction or point where I'm going wrong, it will be of great help.

Apache .htaccess rules:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-z]+)/([a-z]+).html$ /products/code_asset_2019.php?page=$1&filename=$2 [L]

RewriteRule ^(.*)/(.*\.php)$ /products/code.php?page=$1
RewriteRule ^(.*).html$ /products/code.php?page=$1

</IfModule>

Directory structure: root /var/www/html;

inside

/html

, we have /products folder which contains code_asset_2019.php and code.php

My converted rules which are not working:

location /products {
      try_files $uri $uri/ @rules;
    }

    location @rules {
    rewrite ^/([a-z]+)/([a-z]+)\.html$ /products/code_asset_2019.php?page=$1&filename=$2 last;
    rewrite ^/(.)/(.\.php)$ /products/code.php?page=$1;
    rewrite ^/(.*)\.html$ /products/code.php?page=$1;

    return 404;
}
1

Why not put this instead of everything you have:

rewrite ^/products/([a-z]+)/([a-z]+)\.html$ /products/code_asset_2019.php?page=$1&filename=$2 last;
rewrite ^/products/(.)/(.\.php)$ /products/code.php?page=$1;
rewrite ^/products/(.*)\.html$ /products/code.php?page=$1;
5
  • It's an hour past midnight here and I was about to sleep but just give me 5 minutes, I'm trying this out right now. This issue has been keeping me awake anyway. – Axel Aug 1 '19 at 19:49
  • Danila, you've no idea how thankful I'm to you for your help. This worked. Can you please elaborate a little why the previous rules didn't work? – Axel Aug 1 '19 at 20:02
  • @Axel, I think because in your Apache config RewriteRule ^(, matches to /products because .htaccess was inside the directory, whereas in nginx, the match will always check against full URL. So your example needs rewrite ^/products(... instead of rewrite ^/(. But I have simplified things further: if you need to rewrite a bunch of URLs, you don't need location stuff at all. – Danila Vershinin Aug 1 '19 at 20:06
  • Nice! I get it now. Thank you! If you don't mind, I've one more question. When I was working in Apache, We had a php file which would form questions based on guid of the questions and if the question bank had more than one question, the url would look like: www.mywebsite.com/file.php&group_guid=001,001hG,001IF,001Mp,001Qf,001QH,001Qk,001qk,001tq,0022Q Guid of the questions were separated by comma but in nginx, this is not working. First question would display fine but other questions will not and it will display a Page Not Found 404 error. – Axel Aug 1 '19 at 20:16
  • Can you please have a look at my last comment? – Axel Aug 2 '19 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.