search
HomeBackend DevelopmentPHP TutorialAPACHE的AcceptPathInfo指令使用介绍_PHP

Apache

学习zfdemo的时候提到设置 AcceptPathInfo 指令.

有时我们在做虚拟静态化或者让路径看起来很漂亮的时候,可能会看到http://www.example.com/index.php/html1这样URL地址,而在访问的实际是根目录下的index.php文件,而把/html1做为PATH_INFO环境变量传递给脚本。而对于apache来说上面的地址能否正确运行取决于AcceptPathInfo指令的配置

AcceptPathInfo 指令

说明 是否接受附带多余路径名信息的请求
语法 AcceptPathInfo On|Off|Default
默认值 AcceptPathInfo Default
作用域 server config, virtual host, directory, .htaccess
覆盖项 FileInfo
状态 核心(C)
模块 core
兼容性 仅在 Apache 2.0.30 及以后的版本中可用

此指令决定是否接受在实际文件名(或实际目录中一个不存在的文件)后跟随多余路径名信息的请求。这个多余的路径名信息可以当作PATH_INFO环境变量传递给脚本。

比如说,假设/test/所指向的目录下只包括一个文件:here.html ,那么对/test/here.html/more和/test/nothere.html/more的请求都会将PATH_INFO环境变量设为"/more"。

AcceptPathInfo指令的取值范围:

Off
仅当一个请求映射到一个真实存在的路径时,才会被接受。这样,如上述/test/here.html/more这样在真实文件名后跟随一个路径名的请求将会返回一个"404 NOT FOUND"错误。
On
只要前导路径可以映射到一个真实存在的文件,就可以接受该请求。这样,只要上述/test/here.html能够映射到一个有效的文件,那么对/test/here.html/more的请求就会被接收。
Default
是否接收附带多余路径名信息的请求由其对应的处理器来决定。对应普通文本的核心处理器默认会拒绝PATH_INFO 。而用于伺服脚本的处理器,比如cgi-script和isapi-isa,默认会接受PATH_INFO 。
AcceptPathInfo指令存在的首要目的就是允许您覆盖处理器关于是否接受PATH_INFO的默认设置。这种覆盖是很必要的。比如说,当您使用了类似INCLUDES这样的过滤器来根据PATH_INFO产生内容时。核心处理器通常会拒绝这样的请求,而您就可以用下述的配置使这样的脚本成为可能:

Options +Includes
SetOutputFilter INCLUDES
AcceptPathInfo On

apache 2.0以上中的默认的是没有acceptpathinfo

从APACH2.0.30以上服务器中去掉了acceptpathinfo;如果需要的话需要在http.conf中添加AcceptPathInfo On这一条。即原来的

Options FollowSymLinks includes
AllowOverride None
改成
Options FollowSymLinks includes
AllowOverride None
 AcceptPathInfo On

此指令决定了是否接受包含在某确定文件(或是某现有目录的一个不存在的文件)后附加的路径信息。此路径信息将在脚本里以PATH_INFO环境变量的形式出现。
比如说,假设/test/所指向的目录下只包括一个文件:here.html。那么对/test/here.html/more和/test/nothere.html/more的请求都会得到/more这样的PATH_INFO变量。
AcceptPathInfo指令的三个参数为:
off
仅当一个请求映射到一个真实存在的路径时,它才会被接受。这样,如上述/test/here.html/more这样的在真实文件名后跟随一个路径名的请求将会返回一个404 NOT FOUND错误。
on
如果前面的路径映射到一个真实存在的文件,此请求将被接受。如果/test/here.html映射着一个有效的文件,上例中/test/here.html/more这个请求就会被接受。
default
对于附加路径名的请求的处理方式由其对应的处理器来决定。对应普通文本的核心处理器默认会拒绝PATH_INFO。而用于伺服脚本的处理器,比如cgi-script和isapi-isa,默认会接受PATH_INFO。

PHP中的全局变量$_SERVER['PATH_INFO']是一个很有用的参数,众多的CMS系统在美化自己的URL的时候,都用到了这个参数。

对于下面这个网址:
http://www.test.com/index.php/foo/bar.html?c=index&m=search
我们可以得到 $_SERVER['PATH_INFO'] = ‘/foo/bar.html',而此时 $_SERVER['QUERY_STRING'] = 'c=index&m=search';
通常,我们最初开始PHP程序编写的时候,都会使用诸如: http://www.test.com/index.php?c=search&m=main 这样的URL,这种URL不仅看起来非常奇怪,而且对于搜索引擎也是非常不友好的。很多搜索引擎收录的时候,都会忽略Query String之后的内容,google虽然不会忽略Query String,但是对于其他不含Query String的页面,会给于比较高的PR值。

下面是一段解析PATH_INFO的非常简单的代码:

复制代码 代码如下:
if( !isset( $_SERVER['PATH_INFO'] ) ){
$pathinfo = 'default';
}else{
$pathinfo = explode('/', $_SERVER['PATH_INFO']);
}
if( is_array($pathinfo) AND !empty($pathinfo) ){
$page = $pathinfo[1];
}else{
$page = 'a.php';
}
require "$page.php";
?>


php文件名后加斜线“/”不能正常访问,报not found错误
系统坏后,重装系统后,配置php环境。用的软件和以前的版本都相同。

环境配置好后,因工作项目都是单入口文件,index.php文件后边加了斜线才能进入。没换系统前都可以访问,可以排除软件版本问题.

刚想进入工作项目,就报not found 不知为何。测试后得知,php文件名后加斜线“/”不能正常访问

询问多人,未果。google 度娘 未果

找公司一资深php工程师
说是apache有这样一个指令 :AcceptPathInfo

在apache的配置文件里面加上:AcceptPathInfo on 就ok了。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to calculate the total number of elements in a PHP multidimensional array?How to calculate the total number of elements in a PHP multidimensional array?May 15, 2025 pm 09:00 PM

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

What are the characteristics of do-while loops in PHP?What are the characteristics of do-while loops in PHP?May 15, 2025 pm 08:57 PM

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

How to hash strings in PHP?How to hash strings in PHP?May 15, 2025 pm 08:54 PM

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

How to implement array sliding window in PHP?How to implement array sliding window in PHP?May 15, 2025 pm 08:51 PM

Implementing an array sliding window in PHP can be done by functions slideWindow and slideWindowAverage. 1. Use the slideWindow function to split an array into a fixed-size subarray. 2. Use the slideWindowAverage function to calculate the average value in each window. 3. For real-time data streams, asynchronous processing and outlier detection can be used using ReactPHP.

How to use the __clone method in PHP?How to use the __clone method in PHP?May 15, 2025 pm 08:48 PM

The __clone method in PHP is used to perform custom operations when object cloning. When cloning an object using the clone keyword, if the object has a __clone method, the method will be automatically called, allowing customized processing during the cloning process, such as resetting the reference type attribute to ensure the independence of the cloned object.

How to use goto statements in PHP?How to use goto statements in PHP?May 15, 2025 pm 08:45 PM

In PHP, goto statements are used to unconditionally jump to specific tags in the program. 1) It can simplify the processing of complex nested loops or conditional statements, but 2) Using goto may make the code difficult to understand and maintain, and 3) It is recommended to give priority to the use of structured control statements. Overall, goto should be used with caution and best practices are followed to ensure the readability and maintainability of the code.

How to implement data statistics in PHP?How to implement data statistics in PHP?May 15, 2025 pm 08:42 PM

In PHP, data statistics can be achieved by using built-in functions, custom functions, and third-party libraries. 1) Use built-in functions such as array_sum() and count() to perform basic statistics. 2) Write custom functions to calculate complex statistics such as medians. 3) Use the PHP-ML library to perform advanced statistical analysis. Through these methods, data statistics can be performed efficiently.

How to use anonymous functions in PHP?How to use anonymous functions in PHP?May 15, 2025 pm 08:39 PM

Yes, anonymous functions in PHP refer to functions without names. They can be passed as parameters to other functions and as return values ​​of functions, making the code more flexible and efficient. When using anonymous functions, you need to pay attention to scope and performance issues.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment