0

How can i convert my htaccess rules here

RewriteRule ^/?([^/]+)/([^/]+)/?$ page-1.php?a=$1&b=$2 [L,QSA]
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/?$ page-2.php?a=$1&b=$2&c=$3 [L,QSA]

into nginx rules? I've tried using converters but i got the following

winginx
location / { 
rewrite ^/?([^/]+)/([^/]+)/?$ /page-1.php?a=$1&b=$2 break; 
rewrite ^/?([^/]+)/([^/]+)/([^/]+)/?$ /page-2.php?a=$1&b=$2&c=$3 break;
}


anilcetin
rewrite ^/?([^/]+)/([^/]+)/?$ /page-1.php?a=$1&b=$2 last;
rewrite ^/?([^/]+)/([^/]+)/([^/]+)/?$ /page-2.php?a=$1&b=$2&c=$3 last;

But none of them both actually worked, What is the problem here?

in my code here a page like

domain.com?page-1.php?a=a&b=b would be turned into domain.com/a/b

domain.com?page-2.php?a=a&b=b&c=c into domain.com/a/b/c

Axon
  • 3
  • 2
  • 2
    People need to understand Apache rewrite syntax to answer your question. If you could clearly describe the rewrite rule you might have more luck with an answer. If you have more information please edit your post, don't just add comments. – Tim Aug 06 '17 at 04:10
  • @Tim Described the rule a little, As how i want the result to be in `NGINX` – Axon Aug 06 '17 at 09:50
  • When I wanted to convert Apache rewrite rules into Nginx I tried the automatic converters first, but they didn't work. I learned about Nginx rewrite rules, regular expressions, and solved the problem myself. It took a few hours, but it was quite interesting. Here at SF we really want to see that you've tried to solve a problem yourself - we're here to help people with hard problems, rather than to write configurations for people. Do some reading, have a go yourself, if you can't solve it come back and tell us where you got to, someone will help them. [RegEx 101](https://regex101.com/) helps. – Tim Aug 06 '17 at 19:10
  • @Tim My regular expressions are correct, I only want to understand how to use the `location / {}` part using my `regex`, Like do i have to type a different location for each page or all in one? – Axon Aug 06 '17 at 20:27

1 Answers1

0

The Problem

Your comments say you're happy with your regular expressions and rewrite statements, and just need help with the parts around Nginx and location blocks.

A quick glance at them doesn't fill me with confidence that you have the rewrites correct. I would expect something more like this - note this will certainly be incorrect, is definitely not optimal, and may not even pass "nginx -t" checks.

rewrite ^/([0-9a-zA-Z_\-\s\+]+)?a=([0-9a-zA-Z_\-\s\+]+)&b=([0-9a-zA-Z_\-\s\+]+)*c=([0-9a-zA-Z_\-\s\+]+)  /$2/$3/$4;

The general idea here is a capture group for each part of the URL you care about, plus one up front that's not used. That first one is just because I'm not great at regex and it's easier to copy and paste something from one of my own configurations that might work.

Nginx Locations and RegEx

Nginx rewrite can be in the server or location context, and work in the order in the configuration file. For you I'd probably put them near the top of the site config file, above the location blocks

server {
  server_name example.com;
  listen 80;

  rewrite ^/?([^/]+)/([^/]+)/?$ /page-1.php?a=$1&b=$2 break; 
  rewrite ^/?([^/]+)/([^/]+)/([^/]+)/?$ /page-2.php?a=$1&b=$2&c=$3 break;

  location / { 
    # Whatever you want in here to handle the rewritten or normally requested URLs
    # root /var/www/html;
    # proxy_pass 1.2.3.4;
  }

  location /a {
    # If you want to handle specific URLs you can do it in another location block
  }
}

Overall comment on your question is it's not very precise, and doesn't demonstrate that you've made any effort to solve the problem yourself.

Tim
  • 31,888
  • 7
  • 52
  • 78