1. 程式人生 > >關於apache開啟rewrite模式後對URL解析測試

關於apache開啟rewrite模式後對URL解析測試

我們知道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 [email protected] 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.


因此,根據以上測試總結得出:

  1. 重寫規則對動態指令碼php支援較好(只要存在著該檔名,就可以訪問到),對靜態html不可使用localhost/farm/123.html/Home/Hello這樣的路徑(會丟擲404錯誤)。
  2. 開啟重寫模式訪問類似這樣的路徑localhost/farm/123.txt/Home/Hello,除了域名之外部分,第一“.”代表了檔案,將優先執行該檔案(123.txt如果存在的話!)

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.