Rewriting with lighttpd - how to remove file extensions

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 00:31:10

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" )

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.

natbro

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" )

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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!