2

I would like to use lighttpd's mod_rewrite to allow requests without a specific file extension. For instance, I would like the following mappings to automatically work:

  • Requesting for "/index" would serve "/index.php".
  • "/dir/file" => "/dir/file.php"
  • "/dir/file?args" => /dir/file.php?args"

Can this be easily done with a single rewrite rule for a given extension (e.g. ".php")?

ssn
  • 2,727
  • 6
  • 27
  • 37

4 Answers4

3

Cassy and natbro got this very nearly right, but as user102008 commented, this erroneously rewrites any directory index. Adding a url.rewrite-once matching anything ending with a '/' seems to make it work.

url.rewrite-once = (  "^(.*)/$" => "$1/" )
url.rewrite-if-not-file = ( "^([^?]*)(\?.*)?$" => "$1.php$2" )
Fred Smith
  • 31
  • 3
1

Without having tested it, but you can give it a shot:

url.rewrite-once = ( 
  "^([^?]*)(\?.*)?$" => "$1.php$2",
)

Basically it means

  • take everything but a question mark
  • and, if exists, take the question mark and everything following

and you rewrite it to the first part, include the .php and add the last part again.

Again: I haven't tested it yet.

Dan Soap
  • 10,114
  • 1
  • 40
  • 49
0

cassie's answer above is just about right. i would suggest dropping the trailing comma and using url-rewrite-if-not-file (available since 1.4.x lighttpd). this lets you serve other files that exist in the same directory without them getting rewritten.

url.rewrite-if-not-file = ( "^([^?]*)(\?.*)?$" => "$1.php$2" )
Community
  • 1
  • 1
natbro
  • 1,038
  • 12
  • 13
-1

yes

^(.*).php $1 [L,R,NC,QSA]

that would be for .htaccess in a directory

^/(.*).php http://same.site/$1 [L,R,NC,QSA]

where your domain is 'same.site' because it needs to redirect for the URL to change (as opposed to proxy)

GeeKieR
  • 35
  • 2
  • 1
    That seems like the correct approach using Apache's mod_rewrite. However, I am looking for a solution for lighttpd. – ssn Nov 23 '09 at 23:10
  • err, sorry I thought the pattern & syntax were the same I've only dabbled with lighttpd http://forum.lighttpd.net/topic/504 – GeeKieR Nov 24 '09 at 06:14