Project

General

Profile

[ANSWERED] mod_rewrite/lighttpd doesn't support conditional variable change?

Added by suisse00 over 4 years ago

Hi!

So I'm setting up a small http server for my own small projects and end-up needing to use rewrite to redirect any request to a common page.
No issue, this was pretty straight forward; everything is into /var/www/public/<myprojectname> but lighttpd is setup to use /var/www/public as the root.

Now I want to setup a DNS to use /var/www/public/<myprojectname> as the root, and I want to write my rewrite only once so it will be shared with the DNS and just from the server ip.

So I created a conditional variable for when traffic comes from my dns and used this variable in my "requested" file and changed the server.document-root to match the new destination.

But for whatever reason, it is like both my overrided variable and document-root (I'm assuming the file I request from the rewrite module use the document-root as the base path) isn't used.

I join a configuration file with annotation of my tests/results.


Replies (3)

RE: mod_rewrite/lighttpd doesn't support conditional variable change? - Added by gstrauss over 4 years ago

lighttpd evaluates variables at startup, not at runtime. If you want to program, then please use lua (examples) However, I think you're mixing up many different concepts and making a complex mess. HTTP Host header is different from document root on disk. mod_rewrite operates on URLs, not paths on disk.

What you are trying to do might be accomplished solely with symlinks on disk.

Or, you might try simplifying the config to something like:

$HTTP["host"] != "mydns.com" {
    url.rewrite-once = ( "^(/[^/]+)(.*)" => "$1/hello.php?from=$1&rest_of_url=$2")
}

but even the above is more complex than it needs to be. If Host is != "mydns.com", send everything to hello.php and parse your projects there.

RE: mod_rewrite/lighttpd doesn't support conditional variable change? - Added by suisse00 over 4 years ago

lighttpd evaluates variables at startup, not at runtime.

Ah... so a block (like inside the $HTTP["host"] =~ "mydns.com" inherit from the file variable (does it contains assignation to module as well?) context.
Within the context, it then evaluates everything to get a literal to be ready to be pushed into modules.

However, I think you're mixing up many different concepts and making a complex mess. HTTP Host header is different from document root on disk. mod_rewrite operates on URLs, not paths on disk.

I do make the difference between URL and disk path but the disk path also end up to dictate the URL. (and in this case, as you wrote, the rewrite will internally transform the URL).
My explanation probably sucks, and the configuration I provided is more of a debug configuration than the expected result.

The server.document-root is the disk base path then will be appended the URL path.
I want to always use mydns.com as an alias of a /var/www/public/myProjectA/, so I used the same pattern as in your own exemple.
Except for the disk base path, I want the behavior from both sources (mydns.com and the ip) to be the same.

Then it may be the WTF part that isn't clear at all.
The current parameters I send to hello.php where only debugging information (to know which path the rewrite module took), not the expected result.

If I go to mydns.com, whatever I request I want to use /var/www/public/myProjectA/hello.php.
If I used the IP then anything from <ip>/myProjectA/ should always use /var/www/public/myProjectA/hello.php as well.

That should end up with a configuration like (I didn't tested it) :

$HTTP["host"] =~ "mydns.com" {
  server.document-root = "/var/www/public/myProjectA" 
  url.rewrite-once = ( "^(.*)" => "hello.php?query=$1" )
}

# Assume it is from IP
# Use the default server.document-root which is "/var/www/public/" 
url.rewrite-once = ( "^/myProjectA/(.*)" => "myProjectA/hello.php?query=$1" )

Finally, the thing I want to do that cause this post, and the mess with var.myProjectDir
Remember, I want both mydns.com/.* and <ip>/myProjectA/.* to have the same behavior. The easy solution is to copy all rewrites at the root of my configuration (aka everything below # Assume it is from IP comment) into the mydns.com conditional section and adjust it a little bit.

I'm not a fan of copy/pasting, but I really hate when I can't do only a copy-paste. (using the IP will always need the "myProjectA" directory in the "source" regex and in the destination file.
If I can help me, I will.

But from you wrote it may not be possible (without Lua, or a third party to generate the final configuration file).

Thanks!

RE: mod_rewrite/lighttpd doesn't support conditional variable change? - Added by gstrauss over 4 years ago

My explanation probably sucks, and the configuration I provided is more of a debug configuration than the expected result.

Yes, it does.

The current parameters I send to hello.php where only debugging information (to know which path the rewrite module took), not the expected result.

Your example was very poor and did not convey what you were trying to do.

Please test your own examples before posting configuration snippets. Your rewrite targets must begin with '/'

I'm not a fan of copy/pasting,

If that is your key complaint, and it sounds like it is, then have you considered writing a script which generates your lighttpd.conf and avoids the cut-n-paste? Pick your scripting language of choice. Either generate the lighttpd.conf in advance, or have your lighttpd.conf use include_shell to execute your script and parse the results as lighttpd's config.

Your attempt to "program" within the lighttpd configuration file may be misplaced. lighttpd is supposed to be simple. What is easy to do in lighttpd.conf, you can do in lighttpd.conf. If something is too complex to do in lighttpd.conf, please rethink your design, program it in lua, or generate the desired lighttpd.conf via one or more scripts. lighttpd config is not and does not intend to be a Turing-complete scripting language.

    (1-3/3)