Last updated April 9, 2009. Created by fago on January 8, 2006.
Edited by PaulDing, Amazon. Log in to edit this page.
For those who have stepped up a notch in performance and moved from Apache to Lighttpd, you need to fix you clean URLs. There are two ways to do so:
Using url rewrites
First you can use the following configuration for Lighty's mod_rewrite module:
url.rewrite-final = (
"^/system/test/(.*)$" => "/index.php?q=system/test/$1",
"^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^/([^.?]*)$" => "/index.php?q=$1",
"^/rss.xml" => "/index.php?q=rss.xml"
)The first line ensures that Drupal's clean URL check (when saving Settings) succeeds (Drupal makes an HTTP request for a path of the form /system/test/yLgnwqqUu5cWnvPi4Hrz.png). It's a special case that must be handled separately (read on for the reason).
The two following lines let Drupal handle any URL that doesn't contain a dot. This is significant because we can assume, fairly confidently, that addresses like /node/add are Drupal URLs, but addresses such as /themes/bluemarine/style.css are physical files. So the above configuration will work for all cases where this assumption holds true; if there are exceptions to the rule, they can be manually added to the rewrite configuration.
The last line handles the important exception of rss.xml, a Drupal URL that contains a dot.
See also the related discussion at http://drupal.org/node/20766
Using mod magnet
Second you can use lighty's mod magnet module together with a simple lua scripts. Have a look at the tutorials
http://realize.be/drupal-lighttpd-clean-urls-made-easy or
http://more.zites.net/lighttpd_and_drupal_clean_urls_flexible
This way has the advantage that there are no problems with dots.
Comments
Addition of search-related rewrite
I use the following rule in my lighttpd config:
url.rewrite-once = (
"^/system/test/(.*)$" => "/index.php?q=system/test/$1",
"^/search/node/(.*)$" => "/index.php?q=search/node/$1",
"^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^/([^.?]*)$" => "/index.php?q=$1"
)
lighttpd config
Bear in mind that you can't necessarily just cut and paste the examples. I'm running lighty as my local dev server for several sites on a windows machine and file downloads are set to private. The local path for the server is c:/lighttpd/ and each site is c:/lighttpd/sitename-here/htdocs/. I used the following config:
url.rewrite-final = ("^/sitename-here/htdocs/system/test/(.*)$" => "/sitename-here/htdocs/index.php?q=system/test/$1",
"^/sitename-here/htdocs/([^.?]*)\?(.*)$" => "/sitename-here/htdocs/index.php?q=$1&$2",
"^/sitename-here/htdocs/([^.?]*)$" => "/sitename-here/htdocs/index.php?q=$1",
"^/sitename-here/htdocs/system/files/(.*)$" => "/sitename-here/htdocs/index.php?q=system/files/$1",
"^/sitename-here/htdocs/search/node/(.*)$" => "/sitename-here/htdocs/index.php?q=search/node/$1"
)
New version offers less hacky approach
Since lighttpd 1.4.12 and mod_magnet it has been possible to implement this in a more sophisticated manner (i.e. so dots in a non-file URL won't break it). Full details to follow when I've gone through it.
Docs here: http://trac.lighttpd.net/trac/wiki/Docs%3AModMagnet#complex-rewrites
This is how to do it
In lighttpd.conf:
magnet.attract-physical-path-to = ( "/etc/lighttpd/rewrite_drupal.lua" )
(Also, make sure mod_magnet appears and is not commented out in the modules list).
/etc/lighttpd/rewrite_drupal.lua:
attr = lighty.stat(lighty.env["physical.path"])
if (not attr) then
lighty.env["uri.query"] = "q=" .. lighty.env["uri.path"]
lighty.env["uri.path"] = "/index.php"
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
end
I have rewrite_drupal.lua owned by root and chmod 755.
You need lighttpd >= 4.1.12, configured --with-lua. Debian users, this means you need to be running Etch (currently testing) with lighttpd-mod-magnet installed.
Where'd you get lighty.stat
Where'd you get lighty.stat from? Is that something you pulled in from LuaFileSystem?
EDIT: Ah, I see, it's supposed to be part of mod_magnet since 1.4.13. Except it isn't in the Ubuntu 1.4.13 mod_magnet package for some reason...
EDIT2: For anyone else coming along, the default Edgy Ubuntu 1.4.13 package for lighttpd is actually only RC13, so I guess that's why lighty.stat isn't in there. The newer packages (full 1.4.13 release) work fine.
mod_magnet script and pagers?
Hi - currently playing with mod_magnet and your lua code (cheers!) - seem to be experiencing problems with pagers, eg:
http://example.com/node?page=2
(which is perhaps expanding to http://example.com/?q=node?page=2 instead of http://example.com/?q=node&page=2). Any ideas?
---
paul byrne
paul.leafish.co.uk | www.leafish.co.uk
---
paul byrne
paul.leafish.co.uk | www.ixis.co.uk
It would be a problem if there is another query string already
more safe way to rewrite url will look like this:
attr = lighty.stat(lighty.env["physical.path"])
-- we couldn't stat() the file so we need to rewrite
if (not attr) then
-- physical path should be a Drupal's main index.php
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .."index.php"
-- if there is another query string already, prepend it with ours
if(lighty.env["uri.query"]) then
lighty.env["uri.query"]="q="..lighty.env["physical.rel-path"].."&"..lighty.env["uri.query"]
else
-- initial query string was empty, we generate our own
lighty.env["uri.query"]="q="..lighty.env["physical.rel-path"]
end
end
/Michael
/Michael
I referred this url
I referred this url http://more.zites.net/lighttpd_and_drupal_clean_urls_flexible and implemented this for clean urls . It works fine. I got one more thing to be completed. As of now we have two kind of setups in subdirs. First,we have subdirectory in /documentroot/subdirectories . For this setup http://more.zites.net/lighttpd_and_drupal_clean_urls_flexible this works fine. Second we have the another set of subdir in /documentroot/edu/subdirectories. I need the configuration for this set up in drupal-lua-script. and also I want to rewrite the url to www.example.com/edu/sitename to www.example.com/sitename ? Could you help me on
mod-magnet on ubuntu
If you are running Ubuntu Feisty, the mod-magnet is a separate package: lighttpd-mod-magnet.
I followed the instructions at http://nordisch.org/2007/2/6/drupal-on-lighttpd-with-clean-urls
Worked like a charm, even on virtual hosts.
Hope this helps someone with this.
Zanlus
I vouch
The linked configuration works great. Unfortunately, it only works if drupal is in the webroot and not a subdirectory. It does work great though.
Simpler url rule that works with dots
For setups with drupal in subdirs:
url.rewrite = ( "^/([^/]*)/(?!(misc|theme|module|site))([^?]*)\??(.*)$" => "/$1/index.php?q=$3&$4" )
For drupal in the webroot (untested, but should work):
url.rewrite = ( "^/(?!(misc|theme|module|site))([^?]*)\??(.*)$" => "/index.php?q=$2&$3" )
It guesses the static files (images, css, js, uploaded files[usually in the site subdir]) by not matching on the subdirectories were these are usually located (rather than by skipping paths with a dot. It works with appended query strings (e.g. destinations for redirects and pager links).
This setup work perfectly for
This setup work perfectly for me. Thanks! Drupal + Lighty is the best!
Everything I'm not, makes me everything I am
I've described a flexible
I've described a flexible way to support clean-URLs by using the lua-script. So there also no troubles with dots :)
http://more.zites.net/lighttpd_and_drupal_clean_urls_flexible
Woohoo!
Thanks fago, this is the only lua script that worked for me right away, no modifications.
Lighttpd clean url rewrite rules wanted.
I referred this url http://more.zites.net/lighttpd_and_drupal_clean_urls_flexible and implemented this for clean urls . It works fine. I got one more thing to be completed. As of now we have two kind of setups in subdirs. First,we have subdirectory in /documentroot/subdirectories . For this setup http://more.zites.net/lighttpd_and_drupal_clean_urls_flexible this works fine. Second we have the another set of subdir in /documentroot/edu/subdirectories. I need the configuration for this set up in drupal-lua-script. and also I want to rewrite the url to www.example.com/edu/sitename to www.example.com/sitename ? Could you help me on
Ignore this post
Using the mod-rewrite,
With dots and @s it will display 404 error.
Edit. @s works too, pardon me.
Newer Versions: Simple solution with mod_rewrite
New versions of lighttpd support a new way to do rewriting, so now there is a really simple method that should work as well as the solution for Apache.
I currently use this:
url.rewrite-if-not-file = ("^\/([^\?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^\/(.*)$" => "/index.php?q=$1"
)
More Clarification on New Simple Solution
I have tried the above and it did not work 500 internal error.
I am currently using the following please can you suggest a better or more simple method to do this:
I have the following to the lighttpd.conf file:
magnet.attract-physical-path-to = ( "/etc/lighttpd/drupal.lua" )My LUA file is drupal.lua in /etc/lighttpd:
attr = lighty.stat(lighty.env["physical.path"])
if (not attr) then
if (not lighty.env["uri.query"]) then
lighty.env["uri.query"] = "q=" .. lighty.env["uri.path"]
else
lighty.env["uri.query"] = "q=" .. lighty.env["uri.path"] .. "&" .. lighty.env["uri.query"]
end
lighty.env["uri.path"] = "/index.php"
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
end
Please advise of any suggestions to do this in a better way.
removed
removed
Seems to work fine for me …
Is use this for a lot of drupal installations since about one year: It works like a charm so far. rewrite-if-not-file is quite a new directive, so maybe you have a too old lighttpd?
in Drupal 7 this
in Drupal 7 this rule:
url.rewrite-if-not-file = (
"^\/([^\?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^\/(.*)$" => "/index.php?q=$1"
)
not work properly, when attach and upload image to article with image Field
Who write write rule ?
Support for dots
Using the rewrite as described above what else must I do to ensure thta it will function properly dots in the URLs. I am unable to use the lua / mod_magnet method do to my servers lighttpd not compiled with the module.
I am therefore forced to do the rewrite method but do not want to break stuff like image fields etc..
Thanks.
Looking for a Lighttpd rewrite that works with Drupal 7's images
I have also the problem of my Lighttpd rewrite rule conflicting with Drupal 7's image styling :(. For now I will have to turn off clean url's anytime I upload image content, which sucks big time. Because I will soon have multiple users adding pictures and do not want to keep clean urls off.
If anyone can offer help or lead me as to what exactly is the conflict it would be greatly appreciated.
Much Thanks.
This is my current rewrite rule:
url.rewrite-final = ( "/rss.xml$" => "/index.php?q=rss.xml","^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^/search/(.*)$" => "/index.php?q=search/$1",
"^/([^.?]*)$" => "/index.php?q=$1",
"^/([^.?]*\.html)$" => "/index.php?q=$1",
"^/([^.?]*\.htm)$" => "/index.php?q=$1"
)
~fayola
--------
http://www.fayola.net
this work with drupal
this work with drupal 7 and image field
url.rewrite = ( "^/(?!(misc|theme|module|site))([^?]*)\??(.*)$" => "/index.php?q=$2&$3" )
Good rewrite
It is my rewrite settings:
url.rewrite-if-not-file = ("^/system/test/(.*)$" => "/index.php?q=system/test/$1",
"^/search/node/(.*)$" => "/index.php?q=search/node/$1",
"^/(.*)\?(.*)$" => "/index.php?q=$1&$2",
"^/(.*)$" => "/index.php?q=$1"
)
It fine work with any files, images and styles. If file is not exists, request will be redirected in php and processed by drupal.
what is /test/ ?
what is /test/ ?
/test/ !
The /test/ rule is required for Drupal CleanURL check algorithm.
thank you
thank you
Simpler?
I believe you can ommit the first two rules, since they are only special cases of the last rule. When you delete them you end up with my rules from http://drupal.org/node/43782#comment-3221106
Thank you, you are right. Now
Thank you, you are right. Now rule is simple.
Fix for clean URL using Lighttpd
I see a lot of recommendations and fixes. Thanks but where exactly does for example the code below go to fix the clean URL problem? In lighttpd.conf? and where in lighttpd.conf at the end of the code?
Also I didn't see anyone stating that these fixes allowed you to select whether you can have Clean URL on or off in Drupal.
url.rewrite-final = ( "/rss.xml$" => "/index.php?q=rss.xml",
"^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^/search/(.*)$" => "/index.php?q=search/$1",
"^/([^.?]*)$" => "/index.php?q=$1",
"^/([^.?]*\.html)$" => "/index.php?q=$1",
"^/([^.?]*\.htm)$" => "/index.php?q=$1",
)
martinwillb resolved
$HTTP["host"] == "blog.mamalibre.com.ar" {status.status-url = "/server-status"
server.document-root = "/media/Disco160/www/blog.mamalibre.com.ar"
server.dir-listing = "disable"
url.rewrite-once = (
"/rss.xml$" => "/index.php?q=rss.xml",
"^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
"^/search/(.*)$" => "/index.php?q=search/$1",
"^/([^.?]*)$" => "/index.php?q=$1",
"^/([^.?]*\.html)$" => "/index.php?q=$1",
"^/([^.?]*\.htm)$" => "/index.php?q=$1", )
}
works perfect
http://domain.bla.bla/admin/settings/clean-urls
Web Hosting
http://mamalibre.com.ar/
Server Voip
http://mumble.com.ar/
this worked fine for me in
this worked fine for me in d7, really recommended, i had alot of problems with other settings