Lighttpd rewrite rules

Hi OXID community,

we are trying to set up an OXID eShop but didn’t manage yet to convert the .htaccess rewrite rules into lighttpd config successfully.
Actually we even pass the first installation step because of the “apache mod_rewrite module” test.

After playing around a lot I cleared the config and started from the beginning.
Shouldn’t these rules at least let me pass the install test? (they actually don’t)


url.rewrite-one = (
	"^/admin/test\.php$" => "/admin/test.php?mod_rewrite=1",
	"^.*(\.html|\/)$" => "oxseo.php"
)

Does anybody have any experience running an OXID eShop under lighttpd?
A final solution would make a good and very valuable WiKi article.

Thanks in advance,
Henning256

environment infos:
gentoo Linux
lighttpd 1.4.25
OXID eShop CE 4.2.0_23610

Hello Henning,

right, this is a quite interesting issue.
Please check
http://svn.oxid-esales.com/tags/CE-4.2.0-23610/core/oxsysrequirements.php

which is used for mod_rewrite check in the installation routine. Maybe the appropriate function is very apache specific :wink:

Regards

Hello Marco,

thanks for your reply.

I have a really stupid mistake to admit. I made a little spelling mistake:

url.rewrite-one

has to be of course:

url.rewrite-once

Now admin/test.php returns “mod_rewrite_on” as it should but I still don’t pass the test.
Checking oxsysrequirements.php next (good hint, I should have thought so far myself) and will update you about my results.

Cheers,
Henning

[QUOTE=Henning256;23894]
Checking oxsysrequirements.php next (good hint, I should have thought so far myself) and will update you about my results.[/QUOTE]
Cool, I appreciate it :slight_smile:
If you feel fancy, put it straight to the wiki and reference to it in this forum thread.

Rgds

Hmm,

here is the function that bothers me:

    /**
     * Checks if mod_rewrite extension is loaded
     *
     * @return integer
     */
    public function checkModRewrite()
    {
        $iModStat = null;
        $sHost   = $_SERVER['HTTP_HOST'];
        $sScript = $_SERVER['SCRIPT_NAME'];
        if ( $sScript && $rFp = @fsockopen( $sHost, 80, $iErrNo, $sErrStr, 10 ) ) {
            $sScript = str_replace( basename($sScript), '../admin/test.php', $sScript );

            $sReq  = "POST $sScript HTTP/1.1
";
            $sReq .= "Host: $sHost
";
            $sReq .= "User-Agent: oxid setup
";
            $sReq .= "Content-Type: application/x-www-form-urlencoded
";
            $sReq .= "Content-Length: 0
"; // empty post
            $sReq .= "Connection: close

";

            $sOut = '';
            fwrite( $rFp, $sReq );
            while ( !feof( $rFp ) ) {
                $sOut .= fgets( $rFp, 100 );
            }
            fclose( $rFp );

            $iModStat = ( strpos( $sOut, 'mod_rewrite_on' ) !== false ) ? 2 : 0;
        } else {
            if ( function_exists( 'apache_get_modules' ) ) {
                // it does not assure that mod_rewrite is enabled on current host, so setting 1
                $iModStat = in_array( 'mod_rewrite', apache_get_modules() ) ? 1 : 0;
            } else {
                $iModStat = -1;
            }
        }
        return $iModStat;
    }

I wrote a test file and included the function.
It runs the then part. So it calls the /admin/test.php script and checks if it returns “mod_rewrite_on” but even tho it does so in my browser the function returns false.

So it seems I am missing something here.
Doing some debugging and will be back.

Hmm (again),

the socket seem to return

HTTP/1.1 200 OK
Content-type: text/html
Connection: close
Transfer-Encoding: chunked
Date: Mon, 01 Feb 2010 22:07:15 GMT
Server: lighttpd/1.4.25

f
mod_rewrite_off

What the frak can cause the sockets request to not fit the filter while the same request from my Firefox does?

Testing lynx from the same server the script is running on says:

mod_rewrite_on

Any suggestions what other test I could run?

Regards,
Henning

Good news, everyone.

I passed the test!

Because the script calls the test.php script like “/setup/…/admin/test.php” it didn’t match the rule because I had a ^ (stands for beginning of line) in the regex.
Meaning the rule as to be like this:

url.rewrite-once = (
	"/admin/test\.php$" => "/admin/test.php?mod_rewrite=1"
)

Not finished yet, but I will post here when I translated all rules.

I begin to believe I will finally get OXID running on lighttpd… :wink:

Thanks for your participation so far,
Henning

Hi,

I also tried to run Oxid on a lighttpd server.

It took me some time but here are the rules, that seem to work for me. Maybe this is usefull for someome or maybe you have improvements?



# If the shop runs in a subdirectory, set this to /yourdirectory/
var.oxidpath = "/"

url.rewrite-once = (

	# Make the system requirements check work
	"^" + oxidpath + ".*oxseo.php\?mod_rewrite_module_is=off$" => oxidpath + "oxseo.php?mod_rewrite_module_is=on",

	# Exclude the admin, core, export... directories from further rewriting
	"^(" + oxidpath + "(admin|core|export|modules|out|setup|tmp|views).*)$" => "$1"

)

url.rewrite-if-not-file = (

	# Exclude pictures and js files from further rewriting
	"^(" + oxidpath + ".*(\.jpg|\.css|\.pdf|\.doc|\.gif|\.png|\.js))$" => "$1"

)

url.rewrite-repeat-if-not-file = (

	# Add an ending slash to all requests, that don't have an ending slash
	"^(" + oxidpath + ".*)(?<!/)$" => "$1/",

	# Redirect all requests ending with a slash or .html to the oxseo.php script
	"^(" + oxidpath + ".*(/|\.html))$" => oxidpath + "oxseo.php"
)

Regards,
Alex.

Update: Fixed a little bug, I had one path hardcoded

Hi astehlik,

good work and thanks for sharing. :slight_smile:
My OXID shop ist is now running on an other server (Apache) but I am curious to try your solution later.

Best regards,
Henning

Hello @Henning256 and @astehlik

Thanks for the interesting trials. I would be keen to hear if either of you have a fully working system on lighttpd.

On another note, how about going through a setup process in Apache, getting the shop to work completely, and then switch the server to lighttpd once the shop works. Agreed the .htaccess file still has to be adapted for URL-rewriting.

Regards
Ashant

Hi Ashant,

I have an oxid shop running on lighttpd with the settings I posted above.

Works quite nice :slight_smile:

Kind regards,
Alex.

Using the above posted code from ‘astehlik’ I too now have an OXID shop running on Lighty. I did not have to bother with the install problems as I’m simply moving an existing shop that has been installed with Apache.

Thanks astehlik! Saved me a lot of time making the switch from Apache to Lighty.

For anybody else that may try this, don’t forget to check/change permissions on the sessions folder used by php (session.save_path in php.ini) or lighty may not be able to write session data.

This is tricky problem. Who solve?

Getting MS Office 2007 fonts into Linux
outlook 2010Outlook 2010 Exploitability Index Makes Its Debut

I took the re-write rules posted here by astehlik w/o a grain of salt a few months ago and never looked back. However, since they appear to break down as of 4.5.1 I’ve been forced to look again and this time look much closer. This revealed some flaws and/or discrepancies in the rules relative to those used for Apache. For example, query strings in URIs get dropped (applies to all versions) and pics will not generate (as of 4.5.1).

Below I’ve rewritten the rewrite rules (ha ha) to more closely match the original Apache rules. The only thing I could not reproduce accurately was Apache’s ability to re-direct conditionally against file existence. I’ve mentioned the work-around for this in the comments below. Haven’t run these tests on a production server yet (but will soon) so use at your own risk, and check if you have to repair line breaks if you copy from this post.

FOR 4.5.1 AND ABOVE:


If the shop runs in a subdirectory, set this to /yourdirectory/

var.oxidpath = "/"
var.accessiblefiles = “.html|.jpg|.css|.pdf|.doc|.gif|.png|.js|.htc”

Block TRACE|TRACK http request methods

$HTTP[“request-method”] =~ “^(TRACE|TRACK)” {
url.access-deny = ( “” )
}

Make the mod_rewrite system requirements check work and stop further rewrites

url.rewrite-once = (
"^" + oxidpath + “.*oxseo.php?mod_rewrite_module_is=off$” => oxidpath + “oxseo.php?mod_rewrite_module_is=on”,
)

If actual file does not exist AND not accessing an internal directory AND not an accessible

file AND not already eligible for seo processing (ends with / or .html) then assume that uri

is seo eligible but missing trailing slash. Add slash and redirect (permanently).

url.rewrite-if-not-file = (

# Add missing slash then use $0 to stop further processing and go directly to redirect rule.
"^("+oxidpath+"(?!admin|core|export|modules|out|setup|tmp|views)[^?]*)((?&lt;!/|.html)\?.*)?(?&lt;!/|"+accessiblefiles+")$" =&gt; "$0",

)

1. If actual file does not exist AND not accessing an internal directory AND and uri is eligible

for seo processing (ends with / or .html), then send request to oxseo.php.

2. Send non-existing images to image generator script.

url.rewrite-repeat-if-not-file = (

# Request meets requirements for oxseo.php AND has a trailing slash
"^("+oxidpath+"(?!admin|core|export|modules|out|setup|tmp|views)[^?]*(?:/|.html))((?&lt;=/|.html)\?.*)?$" =&gt; oxidpath+"oxseo.php$2",

# Run php script to generate pics not yet generated
"^(" + oxidpath + "out/pictures/" + ".*(\.jpg|\.gif|\.png)" + ")$" =&gt; oxidpath + "core/utils/getimg.php",	

)

If trailing slash is missing from a oxseo.php eligible path, add it and redirect permanently.

IMPORTANT NOTE/WORK-AROUND:

This rule can’t be made conditional upon file existance, like with Apache, so will catch undesired requests such as to

existing php files. For example, without the file exists check, this rule will erroneously add a trailing slash to a

oxseo.php request, as well as any other .php request, and break seo functionality. As a work-around, modifying rule

to ignore all php requests. This should cover existing files accessed by site since all other relavent extensions are

already ignored.

Note: A $PHYSICAL[“existing-path”] field will be added to Lighty 1.5 that could fix this.

url.redirect-code = 301
$HTTP[“host”] =~ “(.)" {
url.redirect = (
# Original rule:
#"^("+oxidpath+"(?!admin|core|export|modules|out|setup|tmp|views)[^?]
)((?<!/|.html)?.)?(?<!/|"+accessiblefiles+")$" => “http://%1$1/$2”,
# Work-around rule:
"^("+oxidpath+"(?!admin|core|export|modules|out|setup|tmp|views)[^?]
)((?<!/|.html|.php)?.*)?(?<!/|”+accessiblefiles+"|.php)$" => “http://%1$1/$2”,
)
}


FOR 4.5.0 AND LESS:

Replace the url.rewrite-repeat-if-not-file rule as follows.


If actual file does not exist AND not accessing an internal directory AND and uri is eligible

for seo processing (ends with / or .html), then send request to oxseo.php.

url.rewrite-repeat-if-not-file = (
# Request meets requirements for oxseo.php AND has a trailing slash
"^("+oxidpath+"(?!admin|core|export|modules|out|setup|tmp|views)[^?](?:/|.html))((?<=/|.html)?.)?$" => oxidpath+“oxseo.php$2”,
)


[QUOTE=spurvis;71091]I’ve rewritten the rewrite rules…[/QUOTE]

:smiley:

Once these are tested and proofed working - maybe you could share for others?
Feel free to add to the wiki: http://wiki.oxidforge.org/Tutorials

Hi, I am running an light server on android with php nad mysql. i have passed the oxid 4.6. installation.
I have expanded the .htaccess with the rules from above and have set the base directory to /oxid/, which is my shop´s dir.
anyhow, I do not get things to work. maybe you could help me…
I just get the initial shop screen and then 404 not found…
woulkd very much appreciate your help

thank a lot
S.K.

Hi,

on Android, this is cool :slight_smile:
In case your light server is equal to lighttpd, please have a look at the former posts. There’s exactly described how to set the rewrite rules for avoiding 404 errors :slight_smile:

Regards

If you need a lightweight server to run the shop, I recommend considering NGINX. Rewrite rules are easier to understand and there’s also already a tutorial linked to the OXID wiki.

But, for future reference, lighty (lighttpd) does not use .htaccess files.

Hi,
I have already managed to get things running - as far as the frontend.
Now all links are rewritten and I get no 404 errors anymore.
Anyhow, there is a problem with the backend - I can login, but get only [HOME]+[FAVORITES] to see, nothing else.
There must be one more rule to change, I think, but did not find out yet…

You mean - the navigation is not shown on the left side, main frame is empty?
There is a known problem on some enviroments with the XML-encoding, please change

<?xml version="1.0" encoding="ISO-8859-15"?>

to

<?xml version="1.0" encoding="UTF-8"?>

in all XMLs like

/admin/menu.xml
/admin/dynscreen_local.xml
/admin/object_rights.xml
/admin/user.xml

there might be some more as well