Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing Apache Rewrite Rules #105

Open
kepi opened this issue Jun 6, 2020 · 2 comments
Open

Parsing Apache Rewrite Rules #105

kepi opened this issue Jun 6, 2020 · 2 comments

Comments

@kepi
Copy link

kepi commented Jun 6, 2020

I'm not sure if this is out of scope of this project, but I wanted to use it to parse real apache configs to store them in NoSQL database so I can query some info about vhosts.

It looked great untill I hit rewrite rules/conds. Problem with Rewrite rules is that order is important.

Having this conf

RewriteEngine On
RewriteCond  %{HTTP_HOST}  ^([^.]+\.[^.]+)(:\d+)?$
RewriteCond  /domains/domain.com/web/_ !-d
RewriteCond  %{HTTP:X-Forwarded-Proto} =https
RewriteRule  ^(.*)$  https://www.%1$1 [L,R=301,QSA]

RewriteCond  %{HTTP_HOST}  ^([^.]+\.[^.]+)(:\d+)?$
RewriteCond  /domains/domain.com/web/_ !-d
RewriteCond  %{HTTP:X-Forwarded-Proto} !=https
RewriteRule  ^(.*)$  http://www.%1$1 [L,R=301,QSA]

I'm getting:

{
  "rewritecond": [
    "%{HTTP_HOST}  ^([^.]+\\.[^.]+)(:\\d+)?$",
    "/domains/domain.com/web/_ !-d",
    "%{HTTP:X-Forwarded-Proto} =https",
    "%{HTTP_HOST}  ^([^.]+\\.[^.]+)(:\\d+)?$",
    "/domains/domain.com/web/_ !-d",
    "%{HTTP:X-Forwarded-Proto} !=https"
  ],
  "rewriterule": [
    "^(.*)$  https://www.%1$1 [L,R=301,QSA]",
    "^(.*)$  http://www.%1$1 [L,R=301,QSA]"
  ],
  "rewriteengine": "1"
}

Which is simply not possible to convert back to working config. I'm not sure what would be best approach.

@Yoda-BZH
Copy link

I have the same issue.

I tried toggling several options (forearray, namedblock, mergeduplicate*), but could get proper rewritecond/rules parsing.

Thank you.

@BigBlueHat
Copy link

Maybe output like this would do the trick:

{
  "rewriteengine": 1,
  "rewrites": [
    {
      "cond": [
        "%{HTTP_HOST}  ^([^.]+\.[^.]+)(:\d+)?$",
        "/domains/domain.com/web/_ !-d",
        "%{HTTP:X-Forwarded-Proto} =https"
      ],
      "rule": [
      ]
    },
    {
      "cond": [
        "%{HTTP_HOST}  ^([^.]+\\.[^.]+)(:\\d+)?$",
        "/domains/domain.com/web/_ !-d",
        "%{HTTP:X-Forwarded-Proto} !=https"
      ],
      "rule": [
        "^(.*)$  http://www.%1$1 [L,R=301,QSA]"
      ]
    }
  ]
}

I realize that changes the key names, but it also keeps the structure consistent--since order matters here in Apache configs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants