使用 lighttpd 重写 - 如何删除文件扩展名

发布于 2024-08-11 11:23:35 字数 249 浏览 8 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失与倦" 2024-08-18 11:23:35

Cassy 和 natbro 的说法几乎是正确的,但正如 user102008 评论的那样,这错误地重写了任何目录索引。添加 url.rewrite-once 匹配任何以“/”结尾的内容似乎可以使其工作。

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

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" )
吃兔兔 2024-08-18 11:23:35

未经测试,但您可以尝试一下:

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

基本上,这意味着

  • 取除问号之外的所有内容
  • ,如果存在,则取问号和后面的所有内容

,然后将其重写到第一部分,包括 .php 并再次添加最后一部分。

再说一遍:我还没有测试过。

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.

蔚蓝源自深海 2024-08-18 11:23:35

上面cassie的答案几乎是正确的。我建议删除尾随逗号并使用 url-rewrite-if-not-file (自 1.4.x lighttpd 起可用)。这使您可以提供同一目录中存在的其他文件,而无需重写它们。

url.rewrite-if-not-file = ( "^([^?]*)(\?.*)?$" => "$1.php$2" )

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" )
假装爱人 2024-08-18 11:23:35

^(.*).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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文