Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

[WEBSERVER] Lighttpd Rewrite Rules

edited October 2010 in Vanilla 2.0 - 2.8
So after much research, trial & error, and a broken mouse or two, I have managed to waffle out some rewrite rules for Lighttpd that seem to be complete.

I'll probably regret saying that this is the end-all, be-all solution for Lighttpd rewrite rules, but I really haven't found any issues thus far and I think the logic behind it is more solid than what has previously been published on the subject.

The trick is that you can't just use mod_rewrite. Instead you have to use mod_magnet. This lets you write lua to define the rewrite rules which gives you far greater control over things. It may be possible to adapt the logic here into rewrite rules, but lua just felt like a more natural way to do it (but then again, I write code for a living).

You can find information about how to install mod_magnet here. Be sure to both enable it in your lighttpd.conf file and to add the following to your domain definition:

magnet.attract-physical-path-to = (server.document-root + "/rewrite.lua" )
Next. Create a file at the root of vanilla (same place .htaccess is) called rewrite.lua. This is important. If you put it anywhere else or name it anything else, the above configuration parameter won't work.

Then, paste the following lua code into your rewrite.lua file (super-commented for your learning pleasure):

-- -- First, let's see if there a physical path to their request. If there -- is, then we probably don't want to rewrite the URL. That would be silly. -- attr = lighty.stat(lighty.env["physical.path"]) -- -- Now we'll only do our dirty work if it didn't exist on the server. -- if (not attr) then -- Save off the query information (i.e. &p=, &DeliveryType=, etc) que = lighty.env["uri.query"] -- Save off the request that they made (i.e. /discussions, /dashboard) req = lighty.env["uri.path"] -- Let's start rewriting! -- What they really meant to request was index.php lighty.env["uri.path"] = "/index.php" -- And the physical relative path to that is actually the same lighty.env["physical.rel-path"] = lighty.env["uri.path"] -- And the full physical path is our doc root plus that lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"] -- And they implicitly asked us to query for p=something (i.e. p=/discussions) lighty.env["uri.query"] = "p=" .. req -- If the request came with it's own whacky query, then we want to append that -- to the end of our own query so that nothing is lost. This is the magic that -- makes sure Ajax popup windows still work, among other things. if (que) then lighty.env["uri.query"] = lighty.env["uri.query"] .. "&" .. que end end
Assuming that all went to plan, all you should have to do is change the parameter in the Vanilla config.php file to:

$Configuration['Garden']['RewriteUrls'] = TRUE;
And then restart Lighttpd. That part's important if you want it to pick up your new rewrite rules.

You should be good to go and hanging out with the cool kids and their Apache-happy friendly URLs. It's good to sit at the big kid's table.

Disclaimer: These are rewrite rules written by me and they have only been tested on my two Vanilla 2 forum sites. I make no guarantees to their effectiveness, so use at your own risk. That being said, they seem to work great for me.

Comments

  • Using it on my forum now, looking good thus far
  • TimTim Operations Vanilla Staff
    Sticky

    Vanilla Forums COO [GitHub, Twitter, About.me]

  • MaximusyaMaximusya New
    edited September 2010
    Here goes my kung fu:
    url.rewrite-if-not-file = ( "^([^?]*)(\?(.*))?$" => "/index.php?p=$1&$3" )
    Notice, that "not found" files would be routed to preinstalled Default404 - that is php backend would be triggered.
    It is ok with me - once I make Default404 route return 404 response code (it returns 200 even if I set "Not Found (404)" in a dashboard)

    To make webserver itself serve 404 pages you can list directories in a separate rewrite-rule and rewrite it to self (i.e. do not rewrite):
    url.rewrite = ( "^/(applications|js|library|plugins|themes|uploads)/(.*)$"=>"$0" ) url.rewrite += ( "^([^?]*)(\?(.*))?$" => "/index.php?p=$1&$3" )

    Surely don't forget to make changes to your config.php:
    $Configuration['Garden']['RewriteUrls'] = TRUE;
    and restart Lighttpd
  • thanks so much, im using it too. lighttpd ftw
  • LincLinc Detroit Admin
    Added to installation documentation and unannouncing.
  • edited February 2011
    We tried lykaon's approach and it was no good. Tried Maximusya's and had success with this line for our subdirectory:

    url.rewrite-if-not-file = ( "\/vanilla\/^([^?]*)(\?(.*))?$" => "/vanilla/index.php?p=$1&$3" )

    Thanks everyone :)
  • We tried lykaon's approach and it was no good. Tried Maximusya's and had success with this line for our subdirectory:

    url.rewrite-if-not-file = ( "\/vanilla\/^([^?]*)(\?(.*))?$" => "/vanilla/index.php?p=$1&$3" )

    Thanks everyone :)
    I've figured out the fix for this, replace this line in lykaon's script...

    req = lighty.env["uri.path"]

    ...with this one:

    req = string.gsub(lighty.env["uri.path"], "^/forums", "")

    Replace "/forums" with your subdirectory, for example: "/boards".
  • This also worked for a Cherokee server.

Sign In or Register to comment.