亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

但是如果是 http://localhost/index 就會提示404錯誤,這是為什么啊

但是如果是 http://localhost/index 就會提示404錯誤,這是為什么啊

叮當貓咪 2023-04-18 18:14:45
我想用 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>


查看完整回答
反對 回復 2023-04-21
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

RewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-dRewriteRule ^(.*)$ index.php/$1


查看完整回答
反對 回復 2023-04-21
  • 2 回答
  • 0 關注
  • 416 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號