Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Rewrite rules for lighttpd

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1 2
Go to End
Author Topic: 14167 Views
  • Damo
    Avatar
    5 Posts

    Rewrite rules for lighttpd Link to this post

    So I downloaded Silverstripe v2.0.2b, installed it under lighttpd on Ubuntu and apart from the mod_rewrite warnings it went well. The issue is that on www.kissick.net.nz it will come up with the welcome default site ok and contact us, about us, etc. but as soon as I go to a URL that has a '?' in it, it breaks and simply returns an empty page. An example of this is http://www.kissick.net.nz/Security/login - upon submitting it attempts to go to http://www.kissick.net.nz/Security/?executeForm=LoginForm

    My regex, mod_rewrite and url.rewrite skills leaves something to be desired.so maybe someone can help me out as I've only guessed based on rules I found in the Windows installer. I currently have the url.rewrite:

    url.rewrite-once = (
    "(?i)(/.*\.(jpe?g|png|css|js|gif|php))$" => "$0",
    "^/(.*)(\?|$)(.*)" => "/sapphire/main.php?url=$1&$3"
    )

    Any suggestions as to where I'm failing? Cheers, - Damian

  • elijahlofgren
    Avatar
    Google Summer of Code Hacker
    222 Posts

    Re: Rewrite rules for lighttpd Link to this post

    I don't know lighttpd nor much regex, but I found this in C:\lighttpd\etc\lighttpd.conf my Windows SilverStripe install:

    #### url handling modules (rewrite, redirect, access)
    $HTTP["url"] !~ "\.pdf$" {
       url.rewrite-once = (
          "^/(.*)(\?|$)(.*)" => "/sapphire/main.php?url=$1&$3"
       )
    }

    Attached is the entire lighttpd.conf file in case you find it useful.

    Hope this helps,

    Elijah

    Edit: Sorry, I didn't read your post closely enough, it seems you already found lighttpd.conf

  • Hans
    Avatar
    3 Posts

    Re: Rewrite rules for lighttpd Link to this post

    You should remove the \ before the "
    I have the next line in lighttpd.conf:

    "^/silverstripe/(.*)(\?|$)(.*)" => "silverstripe/sapphire/main.php?url=$1&$3"

  • Damo
    Avatar
    5 Posts

    Re: Rewrite rules for lighttpd Link to this post

    Hans:
    Oddly enough, when I encased the code in

    it escaped the double-quotes. Have removed the tag pair now and you'll find that it pretty much matches what you have.

    I avoided $HTTP['url'] because I was told that a url.rewrite-once doesn't work inside that conditional. Of course, this seems to be incorrect since the windows installer runs with it fine. I also put the ' => $0' rule because without it, the css styling and images, etc. wouldn't show up. I secretly suspect something odd in my lighttpd setup so I might flatten it and/or just switch to Apache. If I get a chance I will triple-check the error and access logs but last time I checked they didn't help me much.

    Cheers for your guys help so far.

  • Hans
    Avatar
    3 Posts

    Re: Rewrite rules for lighttpd Link to this post

    Sorry, I didn't look at the first post where the \ are not there. Do you use symbolic links ? I noticed some problems when i linked silverstripe-2.0.2b to silverstripe and used the latter name in the rewrite rules. Otherwise I can send you my lighttpd.conf file if you are interested.

  • Damo
    Avatar
    5 Posts

    Re: Rewrite rules for lighttpd Link to this post

    Hehe, I editing my first post to remove the code tag. No symlinks either.
    I grow more confident it is something odd about my particular setup. I will look into it again at some stage. Cheers for the help.

  • robvdl
    Avatar
    Community Member
    5 Posts

    Re: Rewrite rules for lighttpd Link to this post

    I had exactly the same problem and found a solution

    First of all, I just like to say that there is currently no information in the Silverstripe help, or on the site from what I can see, on how to get Silverstripe rewrite rules going with lighttpd. So the only way of figuring the rewrite rules out, is by trying the Windows all-in-one installer, and copying the lighttpd configuration file from there, it looks like others are doing exactly what I had done here.

    The problem is the Windows all-in-one installer seems to be bundled with an old version of lighttpd 1.4.10. The Windows all-in-one installer uses an $HTTP['url'] rule, which no longer seems to work in Lighttpd 1.4.18, either that, or it could be because I am wrapping it in an $HTTP['host'] block.

    I had the same problem with Damo's rule, it worked for any pages without a ? in it, but as soon as you try to log in, things break. I had a really good look at it, and the problem is because of a greedy regex rule.

    Damo's rule was:

    "^/(.*)(\?|$)(.*)" => "/sapphire/main.php?url=$1&$3"

    The problem is the first (.*) is greedy, which means it never gets to the (\?|$) part.

    To fix it, simply change to:

    "^/(.*?)(\?|$)(.*)" => "/sapphire/main.php?url=$1&$3"

    *? is non-greedy, it repeats the previous item zero or more times. Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item.

    Source:

    http://www.regular-expressions.info/reference.html

  • robvdl
    Avatar
    Community Member
    5 Posts

    Re: Rewrite rules for lighttpd Link to this post

    Update 2: This code is obsolete, please check:

    http://doc.silverstripe.com/doku.php?id=installation-on-lighttpd

    Update: Since I last posted this, I have found two issues with my lighttpd config code I had posted:

    1. If you have an image in your assets folder ending on .JPG in capitals instead of .jpg it doesn't work in admin.

    2. Image previews do not show in admin, because they have text after the .jpg part for example: http://yoursite.com/assets/_resampled/AssetLibraryPreview-image.jpg?r=60205

    The solution for #2 is before the $ on the first regex line, insert (.*?)

    The solution for #1 can be done in different ways, if you only want to support .jpeg .jpg .JPEG and .JPG but not less obscure extensions like .JpEg then the following will do (and it will also be faster):

    $HTTP["host"] == "yoursite.com" {
    server.document-root = "/home/yoursite/public_html/"
    url.rewrite-once = (
    "(?i)(/.*\.(jpe?g|JPE?G|png|PNG|css|CSS|js|JS|gif|GIF|php|PHP|x?html?|X?HTML?))(.*?)$" => "$0",
    "^/(.*?)(\?|$)(.*)" => "/sapphire/main.php?url=$1&$3"
    )
    server.error-handler-404 = "/sapphire/main.php"
    }

    If you do want to support more obscure extensions such as .JpEg you will need a much more complex regex, something like this:

    ((j|J)(p|P)(e|E)?(g|G)|(p|P)(n|N)(g|G) ... etc

    But in reality, I don't think anyone is going to use mixed case file extensions, so my first example would probably be much better, and faster too.

    14167 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.