Project

General

Profile

Extensionless PHP

Added by Produkt over 3 years ago

I am trying to convert some NGINX rules to lighttpd (https://stackoverflow.com/a/21911610/7511288). I am intending to be able to leave the ".php" extension off my URLs and the server assumes them. First, it should check for directory first and try typical index files. For example:

example.com -> example.com/index.php
example.com/cca -> example.com/cca/index.php
If example.com/cca/index.php does not exist, should go to example.com/cca.php
example.com/admin?id=1234 -> example.com/admin/index.php?admin=1234
If does not exist, -> example.com/admin.php?id=1234

Here's what I have so far:
url.rewrite-once = ( "^(.*)/$" => "$1/" )
url.rewrite-if-not-file = ( "^([^?]*)(\?.*)?$" => "$1.php$2" )

Currently, example.com/page will go to example.com/page.php but will not find example.com/page/index.php

Any help?


Replies (3)

RE: Extensionless PHP - Added by gstrauss over 3 years ago

With the default lighttpd modules (without any of your rewrite rules), a request for a file returns a file, and a request for a directory will result in a redirect from http://example.com/dir to http://example.com/dir/ if the url-path does not end in '/'. For requests for /dir/, if mod_indexfile is enabled and configured to look for index.php, then mod_indexfile will check for /dir/index.php

If the /admin/ directory exists, then with mod_indexfile configured, lighttpd will look for /admin/index.php, but if that file does not exist, the response will be 403 Forbidden (unless mod_dirlisting is enabled) as /admin/ is a directory.

Now on to your config:

If /admin/index.php does not exist, then you are trying to rewrite the url-path to /admin.php, but your rewrite rule will rewrite to /admin/.php since your rule does not take into account that the url-path is now /admin/

This will work:
url.rewrite-if-not-file = ( "^([^?]*)/(\?.*)?$" => "$1.php$2" )
(and delete your unnecessary url.rewrite-once rule)

RE: Extensionless PHP - Added by Produkt over 3 years ago

Thank you very much. It appears using this new rule I get 404 errors unless I type out the entire filename. example.com/cooks used to take me to example.com/cooks.php but now it does not. I also get 404 errors on directories, example.com/cca gives me a 404 unless I type out example.com/cca/index.php

RE: Extensionless PHP - Added by gstrauss over 3 years ago

lighttpd aims to be able to do simple things with simple configurations. Before I posted, I tested this 4-line lighttpd config:

server.document-root = "/tmp" 
server.modules += ("mod_rewrite")
index-file.names = ("index.txt")
url.rewrite-if-not-file = ( "^([^?]*)/(\?.*)?$" => "$1.txt$2" )

I created echo index > /tmp/admin/index.txt and echo admin > /tmp/admin.txt
When I made a request to http://127.0.0.1/admin I got "index".
When I removed /tmp/admin/index.txt and made the request again, I got "admin".

Most people do not realize that they are their own worst enemy in complicating things. Your problem is somewhere in your configuration, which you have not shared.

    (1-3/3)