关于apache开启rewrite模式后对URL解析测试,apacherewrite


我们知道php的MVC模式离不开$_SERVER['REQUEST_URI']和$_SERVER['SCRIPT_NAME']这两个全局变量。前者用来获取请求的整个URL,后者则用来获取当前执行的脚本文件路径。例如:

我们的访问路径http://localhost/farm/index.php/Home/hell

其中index.php内容如下:

<?php

echo $_SERVER['REQUEST_URI'],'<br />';

echo $_SERVER['SCRIPT_NAME'],'<br />';

我本地执行结果如下:

/farm/index.php/Home/hell
/farm/index.php


开启apache的rewrite模式后,设置AllowOverride all后,添加ThinkPHP官网给的.htaccess文件。文件内容如下:

<IfModule mod_rewrite.c>
RewriteEngine on
#不显示index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>


为了测试apache重写规则效果,我在/farm/目录下放置一个.htaccess内容如上。

然后在/farm/Home/目录下也放了一个.htaccess文件,内容如下:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php/$1 [QSA,PT,L]
</IfModule>

最后在/farm/Home/Hello/目录下放置一个index.html;

我对apache设置为优先识别index.html,然后是index.php.


测试1:当我们访问地址:localhost/farm/Home/Hello时

apache会优先读取/farm/Home/Hello/index.html的内容。


测试2:当我们访问地址:localhost/farm/index.php/Home/Hello时

/farm/目录下新建一个index.php,apache会直接读取/farm/目录下的index.php


测试3:当我们访问地址:localhost/farm/123.html/Home/Hello时

我们的/farm/目录下并不存在123.html,但apache会优先读取/farm/目录下的index.php,因为有重写模式和该目录下的配置文件.htaccess,因该目录不存在123.html文件,仍旧优先加载/farm/目录下的index.php


测试4:当我们访问地址:localhost/farm/123.html/Home/Hello时

我们在/farm/目录下新建一个123.html,可以发现运行后apache会抛出一个404错误

Not Found

The requested URL /farm/123.html/Home/Hello/ was not found on this server.

即并不识别123.html


测试5:当我们访问地址:localhost/farm/123.php/Home/Hello时

我们在/farm/目录下新建一个123.php,可以发现运行后apache是可以识别123.php(内容很简单<?php echo 123; ?>)的,输出如下:

123

即重写规则中,会识别php文件而过滤掉其他文件,即使是httpd.conf中较高的.html后缀都不识别。


测试6:当我们访问地址:localhost/farm/Home/Hell时

注意:URL末尾不是Hello!(即不存在Hell文件夹或者文件),我们在/farm/Home/目录下添加index.html文件。访问以上地址会报错,报错如下:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

为什么?因为刚才我们在/farm/Home/目录下放置的.htaccess配置文件指定的重写规则为:RewriteRule ^(.*)$ user.php/$1 [QSA,PT,L],也就是说会识别user.php,如果存在该文件,在当前目录下找不到文件时,即加载user.php文件。因此。我们在/farm/Home/目录下创建一个user.php文件,内容为:

<?php

echo "hello USER";

再次访问地址:localhost/farm/Home/Hell

即可看到输出:hello USER


测试7:当我们访问地址:localhost/farm/Home/Hell时

我们在/farm/Home/目录下添加index.php文件。内容如下:

<?php
// echo phpinfo();
echo '<br />',$_SERVER['REQUEST_URI'];
echo '<br />',$_SERVER['SCRIPT_NAME'];

输出如下:

/farm/Home/index.php/Hello/
/farm/Home/index.php


测试8:当我们访问地址:http://localhost/farm/Home/123.php/Hell时

我们先改写/farm/Home/下的.htaccess文件,改动如下:

RewriteRule ^(.*)$ user.html/$1 [QSA,PT,L]

/farm/Home/目录下并不存在123.php,输出如下:

Not Found

The requested URL /farm/Home/user.html/123.php/Hell was not found on this server.



因此,根据以上测试总结得出:

A.当该文件不存在时,只要存在.htaccess文件,并配置了重写规则RewriteRule ^(.*)$ user.php/$1 [QSA,PT,L](即标红的文件名),就会加载存在的user.php文件。如果还不存在user.php,则会报错。

B.当文件存在时,但后缀名不为.php等(其他动态脚本后缀未测),则会抛出404文件不存在错误!


而关闭重写模式后,访问诸如localhost/farm/123.html/Home/Hello(在无框架对URL进行路由解析的情况下),即时存在着/farm/123.html和/farm/Home/Hello/index.html文件,也是

Not Found

The requested URL /farm/Home/index.html/Hello was not found on this server.




相关内容

    暂无相关文章