skip to Main Content

I’m trying to transfer a PHP project from apache to lighttpd. I have the following rewrite in the apache:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !.(js|ico|gif|jpg|jpeg|png|css|ttf)$ /index.php?_url=/$1 [QSA,L]

I can’t manage to translate it to lighttpd mod_rewrite.

Here are some of my attempts:

    url.rewrite-if-not-file = (
        # try with two separate matches - when the request ends with file.ext
        ".(?!(js|ico|gif|jpg|jpeg|png|css|ttf))[?]{0,1}(.*)$" => "/index.php?_url=/$1",
        # if the request ends with /
        "/[?]{0,1}$" => "/index.php?_url=/"

#        ".+/(.*)?(.*)" => "/index.php?_url=/$1",

#         "((?!.(js|ico|gif|jpg|jpeg|png|css|ttf)).*)" => "/index.php?_url=/$1"

#        "^([^?(js|ico|gif|jpg|jpeg|png|css|ttf)]+)[?]{0,1}(.*)$" => "/index.php?_url=$1&$2"
    )

With the last version the only difference I see the only difference in PHP’s $_SERVER[‘SCRIPT_NAME’]. It is:
[SCRIPT_NAME] => /v1/en/entity/method/ # with the apache
[SCRIPT_NAME] => /index.php # with the lighttpd

The request itself is:
https://api.local/v1/en/entity/method/

2

Answers


  1. first of all you must place this code in /etc/lighttpd/conf-enabled/10-rewrite.conf after
    server.modules += ("mod_rewrite")
    or in the /etc/lighttpd/conf-enabled/90-vhosts.conf at the corresponding vhost (respectively in the conf-available and point a symlink to conf-enabled)

    you cannot place it in .htaccess

    BUT OK lets admit this is the domain http://www.example.com

    http['HOST'] == "www.example.com"{
        // rewrite the url if there is no such file and
        url.rewrite-if-not-file = (
            "^(.*)!.(js|ico|gif|jpg|jpeg|png|css|ttf)$" => "/index.php?_url=/$1"
        )
    }
    

    this should work the same way as in Apache.
    (I haven’t tested it)

    Login or Signup to reply.
  2. $HTTP["host"] == "www.example.com" {
        # rewrite the url if there is no such file and not a listed extension
        url.rewrite-if-not-file = (
            ".(?:js|ico|gif|jpg|jpeg|png|css|ttf)$" => "",
            ".*" => "/index.php?_url=$0"
        )
    }
    

    https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModRewrite

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search