discord.jse.io" itemprop="description"/>

Jonah's Blog

Jonah's blog is here

So mempler (join our discord server if you don't know him!) is working on a currently undisclosed project on one of my servers and wanted to do some rewriting to a php file.

I never have done much with rewrites ever with anything. I never really cared about ugly URLs anyway with anything I ran in PHP so I just didn't care. On the occasion I needed to run something that did, someone always provided it to me in a .htaccess with it, but I want to use lighttpd. It's just more efficient for what we want. Fight me.

Anyway he wanted something like example.com/path/here to rewrite to example.com/index.php?path=WhateverCameAfterSlashHere as long as no ? appears in the URL (more on that later).

So that is pretty easy. Just do something like "([^?]*)" => "index.php?path=$1"

So what does this do? Well. I used this place and the lighttpd documentation to figure this out. Basically, a * will match anything. Quoting autohotkey.com:

An asterisk matches zero or more of the preceding character, class, or subpattern. For example, a* matches ab and aaab. It also matches at the very beginning of any string that contains no "a" at all.

Anything in the parentheses are a regex group for lighttpd, so anything it matches is provided to $1. Quoting the lighttpd documentation:

If the matched regex contains groups in parentheses, $1..$9 in the replacement refer to the captured text in the matching group "$1" meaning the first group, "$2" the second, and so on.

More on that later when we want to have a second group.

I also wanted it to stop at a ?, which eventually will be used to look like you're really accessing an index.php on the path in the URL and you're going to specify the first parameter that of course is passed to $_GET, even though the path is one too but unsuspecting people won't know that.

With the [^?] before the * we're telling regex to match everything EXCEPT for a ?.

Again, to quote autohotkey.com:

Matches any single character that is not in the class. For example, [^/]* matches zero or more occurrences of any character that is not a forward-slash, such as http://. Similarly, [^0-9xyz] matches any single character that isn't a digit and isn't the letter x, y, or z.

Now we also want to handle other parameters. This is where our second group comes in. Of course, we already included the ? for the path parameter in the rewrite rule where $1 is passed. When people supply a ? it should actually be after an & and the ? shouldn't actually be passed to the requested php file. I did this:

url.rewrite = (
                "([^?|&]*)\?(.*)" => "index.php?path=$1&$2"

As you see, since the ? in regex actually means something (A question mark matches zero or one of the preceding character[...]) I need to escape it with a backslash which isn't a foreign concept to anyone who has dabbled in scripting and programming even a little bit. So this means expect an actual ? character in the URL and then I made a second group that will match *literally *everything with the .*. That's fine though, he can handle it in the PHP code for whatever he wants to do. This means that any other parameters in the URL will be followed with an actual & character in the URL and not something else of course, but that's fine, nothing for lighttpd to handle.

You'll also notice that in my first group where I told it to not match a ? that I specified a | (which means OR, also not a foreign concept for most) followed by an & symbol, so people can't specify additional parameters after an & sign before the ?.

This means we're now resulted with: url.rewrite = ( "([^?|&]*)\?(.*)" => "index.php?path=$1&$2" )

Now the problem is that sometimes he doesn't want to specify other parameters after the ? in the URL, but if you try the rewrite example I provided you'll notice if no ? character is specified in the URL you get a 404 thrown at you as the rule has no match.

I initially then decided to do

url.rewrite = (
                "([^?|&]*)" => "index.php?path=$1"
                "([^?|&]*)\?(.*)" => "index.php?path=$1&$2"

Now as you might expect if you're not retarded like me that won't work. Now parameters are never provided to the PHP file when they are in the URL because the first rule I specified always applies (and of course, doesn't match anything with a? character or an & character)!

The fix is simple. Just reverse them like so:

url.rewrite = (
                "([^?|&]*)\?(.*)" => "index.php?path=$1&$2",
                "([^?|&]*)" => "index.php?path=$1"

This way if a ? exists in the URL then of course, the first rewrite rule applies since it's the first that appears. Otherwise, the second will.

That's for going along with me on my regex learning journey. I know this is probably simple for most people but took me a while to figure out as I've never done it before (aside from fixing my htmly rewrites for lighttpd a while ago which doesn't count!)


- Posted in Web/Technology by with comments

So the server this blog is hosted on is bad.

I mean, I love the thought of a low end box and never have I pushed it to it's fullest potential, but I frequently get emails from statuscake telling me it's down, yet again. It seems to be network, the vm doesn't actually go down, but whatever. I'll say more about that another time.

Anyway, I am moving this site to a Kimsufi dedicated server in time. It's an i3-2130 for $12.99/mo on flash sale. Not an amazing "server" but the price is nice.

I'll still keep this LEB, it will probably host a mirror for stuff and backups, but nothing I will really care much about beyond that.

Perhaps I'll write a more in depth review about it all later.

Anyway since I scooped up a couple of these I'm going to move everything and mirror the data across them, along with publishing some downloads to the BuyVM anycast "CDN" I have so there will be a lot of redundancy when I'm done for everything.

UPDATE: 3:30AM on the 20th and I'm going to bed. https://jseb.ca is moved though (which probably doesn't mean much as it's mostly a screenshot server I use with sharex aside from mirroring some other files, but it had a lot of files (including the recovery bins, some images on this blog and one of the drivers for blackweb), and I'm slow as I talked a lot on discord in between, but it's moved.

Tomorrow is a busy day but perhaps in the evening I'll move some other stuff then actually get onto moving this website. Meanwhile mempler.de has been working on something with all that storage I've gotten my hands onto ;) Hopefully I can also write more about that as time goes on and get more involved into it (and learn more about node.js!!)

Oh yeah.... it's also an opportunity for me to move 100% of my stuff to HTTPS with HSTS. So there's that. Say goodbye to port 80.

Update 2 Nov 21 also early in the morning:

Moved jsebean.com (this site over). Should have been seamless since I'm not hosting any dynamic content here. Just a quick DNS change.