简体   繁体   English

是否可以在没有扩展名的情况下在apache中运行php文件?

[英]Is it possible to have a php file in apache operate without an extension?

I have a file 我有一个文件

http://www.www.com/somefile.php

I would like to be able to get to the same file by going to 我希望能够通过转到同一个文件

http://www.www.com/somefile

But I still need 但我仍然需要

http://www.www.com/directory

to open the file 打开文件

http://www.www.com/directory/index.php

if, in fact, directory is a directory. 实际上,如果目录是一个目录。

This is a typical LAMP setup with Red Hat and Apache. 这是Red Hat和Apache的典型LAMP设置。

Is that possible? 那可能吗? If so, how? 如果是这样,怎么样?

An apache rewrite rule will do this for you. apache重写规则将为您执行此操作。 The only limitation is you can't have a file named "somefile.php" and a directory named "somefile" (though you can still have directories. 唯一的限制是您不能拥有名为“somefile.php”的文件和名为“somefile”的目录(尽管您仍然可以拥有目录。

Here's an example rule that supports multiple filenames (just put in in your .htaccess file). 这是一个支持多个文件名的示例规则(只需放入.htaccess文件中)。

RewriteEngine on
RewriteRule ^(somefile1|somefile2|somefile3) $1.php

Will change /somefile1 to /somefile1.php, /somefile2 to /somefile2.php, and /somefile3 to /somefile3.php. 将/ somefile1更改为/somefile1.php,/ somefile2更改为/somefile2.php,将/ somefile3更改为/somefile3.php。

看看Apache的ForceType。

If you want to disable the need of file extensions on the url globally on your site, you can also add the following to your .htaccess : 如果您想在网站上全局禁用网址扩展名,则还可以将以下内容添加到.htaccess

Options +MultiViews

This will enable Content Negotiation . 这将启用内容协商 But if you want to match only certain files and/or urls, stick with SoapBox's Answer instead. 但是如果你只想匹配某些文件和/或网址,请坚持使用SoapBox的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM