我想用 Apache 的 rewrite 在 php 中實現單一入口,把所有的請求都重定向到 index.php,然后分析 $_SERVER['REQUEST_URI'],構建路由功能。在使用時候時候出現了一個問題,請求其他的地址,比如 http://localhost/a/b/c 可以獲得結果array (size=4)
0 => string '' (length=0)
1 => string 'a' (length=1)
2 => string 'b' (length=1)
3 => string 'c' (length=1)index.php大概是這樣<?php........function analysisUri() { var_dump(explode('/', $_SERVER['REQUEST_URI']));
}echo $_SERVER['REQUEST_URI'];analysisUri();
........virtualhost配置是這樣這個rewrite規則是從網上找到的DocumentRoot /home/www<Directory />
Options FollowSymLinks AllowOverride None #AllowOverride All
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase / #不顯示index.php
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
#RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule></Directory><Directory /home/www/>
Options Indexes FollowSymLinks MultiViews AllowOverride None #AllowOverride All
Order allow,deny
allow from all
DirectoryIndex index.php index.html index.htm
2 回答

PIPIONE
TA貢獻1829條經驗 獲得超9個贊
你的規則有問題。
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
行不通的,正確的應該是 RewriteRule ^(.*)$ index.php?$1 [QSA,PT,L]
。?
才會吧后面的 $1
傳給 index.php,而 /
告訴 apache 查找 index.php/
目錄下的 index 文件,自然是 404。
你可以添加如下配置到 httpd.conf,調試 mod_rewrite,通過查看 rewrite.log 文件可以知道通過 rewrite 得到的路徑到底是什么,完成調試后移除這個配置,否則會有些性能安全方面的影響。
# 輸出調試信息到 rewrite.log<IfModule mod_rewrite.c> RewriteLog "/var/log/apache2/rewrite.log" RewriteLogLevel 3</IfModule>

慕少森
TA貢獻2019條經驗 獲得超9個贊
RewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-dRewriteRule ^(.*)$ index.php/$1
添加回答
舉報
0/150
提交
取消