使用 lighttpd 重写 - 如何删除文件扩展名
我想使用lighttpd的mod_rewrite来允许没有特定文件扩展名的请求。例如,我希望以下映射自动工作:
- 请求“/index”将提供“/index.php”。
- “/目录/文件”=> “/dir/file.php”
- “/dir/file?args”=> /dir/file.php?args"
对于给定的扩展名(例如“.php”),可以使用单个重写规则轻松完成此操作吗?
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")?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Cassy 和 natbro 的说法几乎是正确的,但正如 user102008 评论的那样,这错误地重写了任何目录索引。添加 url.rewrite-once 匹配任何以“/”结尾的内容似乎可以使其工作。
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.
未经测试,但您可以尝试一下:
基本上,这意味着
,然后将其重写到第一部分,包括
.php
并再次添加最后一部分。再说一遍:我还没有测试过。
Without having tested it, but you can give it a shot:
Basically it means
and you rewrite it to the first part, include the
.php
and add the last part again.Again: I haven't tested it yet.
上面cassie的答案几乎是正确的。我建议删除尾随逗号并使用 url-rewrite-if-not-file (自 1.4.x lighttpd 起可用)。这使您可以提供同一目录中存在的其他文件,而无需重写它们。
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.
是
^(.*).php $1 [L,R,NC,QSA]
用于目录中的 .htaccess
^/(.*).php http://same.site/$1 [L,R,NC,QSA]
您的域名为“same.site”,因为它需要重定向才能更改 URL(与代理)
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)