Project

General

Profile

Help with rewrite rules

Added by albertvd about 16 years ago

Hello,

I am busy migrating from Apache 2.2.11 to Lighttpd 1.4.22 and as such is a complete newbie to Lighttpd.

Thus far I have managed to install Lighttpd and PHP as a Fast-CGI. There are several virtual hosts which all need the same rewrite rules as it runs the same application but with different templates to customize the look and feel for each customer. I use mod_simple_vhost for this.

Thus far everything works apart from the rewrite rules.

To test, full debugging (I think) has been switched on:

debug.log-request-header   = "enable" 
debug.log-response-header  = "enable" 
debug.log-request-handling = "enable" 
debug.log-file-not-found   = "enable" 

An example rewrite rule from the .htaccess file:

RewriteRule ^signup/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?version=$1&sid=$2&signup=$3 [QSA,L]

This would rewrite an URL like http://www.mydomain.com/signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html?f=0 to http://www.mydomain.com/index.php?version=4.0.4&sid=3878ad7c19de1855d7724cc05ea2df54&signup=stage_1&f=0

In /etc/lighttpd/lighttpd.conf I have

server.modules              = (
                                "mod_rewrite",
                                "mod_access",
                                "mod_fastcgi",
                                "mod_simple_vhost",
                                "mod_accesslog" )

server.document-root        = "/home/data/www/192.168.1.115/trunk/default/" 

simple-vhost.server-root   = "/home/data/www/" 
simple-vhost.default-host  = "192.168.1.115" 
simple-vhost.document-root = "/trunk/default" 

url.rewrite = (
  "^signup/([^/]*)/([^/]*)/([^/]*)\.html$" => "/index.php?version=$1&sid=$2&signup=$3",
)

When trying to access http://www.mydomain.com/signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html?f=0, I get a HTTP404 and the following in the error log:

2009-04-20 16:47:11: (request.c.294) fd: 6 request-len: 475
GET /signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html?f=0 HTTP/1.1
Host: www.mydomain.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive

2009-04-20 16:47:11: (response.c.221) -- splitting Request-URI
2009-04-20 16:47:11: (response.c.222) Request-URI  :  /signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html?f=0
2009-04-20 16:47:11: (response.c.223) URI-scheme   :  http
2009-04-20 16:47:11: (response.c.224) URI-authority:  www.mydomain.com
2009-04-20 16:47:11: (response.c.225) URI-path     :  /signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.226) URI-query    :  f=0
2009-04-20 16:47:11: (response.c.276) -- sanatising URI
2009-04-20 16:47:11: (response.c.277) URI-path     :  /signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (mod_access.c.135) -- mod_access_uri_handler called
2009-04-20 16:47:11: (response.c.391) -- before doc_root
2009-04-20 16:47:11: (response.c.392) Doc-Root     : /home/data/www/192.168.1.115/trunk/default/
2009-04-20 16:47:11: (response.c.393) Rel-Path     : /signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.394) Path         :
2009-04-20 16:47:11: (response.c.442) -- after doc_root
2009-04-20 16:47:11: (response.c.443) Doc-Root     : /home/data/www/www.mydomain.com/trunk/default
2009-04-20 16:47:11: (response.c.444) Rel-Path     : /signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.445) Path         : /home/data/www/www.mydomain.com/trunk/default/signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.462) -- logical -> physical
2009-04-20 16:47:11: (response.c.463) Doc-Root     : /home/data/www/www.mydomain.com/trunk/default
2009-04-20 16:47:11: (response.c.464) Rel-Path     : /signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.465) Path         : /home/data/www/www.mydomain.com/trunk/default/signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.482) -- handling physical path
2009-04-20 16:47:11: (response.c.483) Path         : /home/data/www/www.mydomain.com/trunk/default/signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.539) -- file not found
2009-04-20 16:47:11: (response.c.540) Path         : /home/data/www/www.mydomain.com/trunk/default/signup/4.0.4/3878ad7c19de1855d7724cc05ea2df54/stage_1.html
2009-04-20 16:47:11: (response.c.120) Response-Header:
HTTP/1.1 404 Not Found
Content-Type: text/html
Content-Length: 345
Date: Mon, 20 Apr 2009 13:47:11 GMT
Server: Lighttpd

It would be much appreciated if someone could point me in the right direction to get the above rewrite rule to work. From there I should be able to fix all the others.

Thanks,

Albert


Replies (1)

RE: Help with rewrite rules - Added by albertvd about 16 years ago

After a lot of fiddling around I finally managed to get it working. Maybe the docs can be updated to be a bit more clear on this.

In the end it was quite simple. All checks had to include the preceeding / which Apache did not need.

So in Apache:

RewriteRule ^signup/([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?version=$1&sid=$2&signup=$3 [QSA,L]

And in Lighttpd:

url.rewrite = (
    "^/signup/(.+)/(.+)/(.+)\.html$" => "/index.php?signup=$3",
)

This still didn't solve the QSA part. I tried the documented (?:\?(.*))? like so:

url.rewrite = (
    "^/signup/(.+)/(.+)/(.+)\.html(?:\?(.*))?" => "/index.php?signup=$3&$4",
)

But that didn't do anything apart from not using the rewrite rule.

I ended up with two rules to get the QSA part:

url.rewrite = (
    "^/signup/(.+)/(.+)/(.+)\.html$" => "/index.php?signup=$3",
    "^/signup/(.+)/(.+)/(.+)\.html\?(.+)$" => "/index.php?signup=$3&$4",
)

So now everything is working BUT I am having an issue where the server seems to lockup from time to time. Nothing in the logs but sometimes it takes forever (60+ seconds) to send back a request. Sometimes it loads up instantly. I'm requesting 32 resources all coming from PHP on one page. I'll look at that on another day.

    (1-1/1)