A. 網址中間的index.php怎麼去掉,看了一些回答不知道怎麼做,希望能回答,解決了多給分,一定的。
使用URL重寫,可以使網站的URL屏蔽index.php這種不友好的網址。
假如你的web伺服器端使用的是apache,那麼你需要開啟mod_rewrite.so模塊,
為網站在apache配置文件中的<Directory>配置節添加如下內容,
Options FollowSymLinks
AllowOverride ALL
Order allow,deny
Allow from all
然後在網站根目錄下創建一個 .htaccess 文件,內容如下
<IfMole mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfMole>
就可以在網址中屏蔽index.php了
B. url怎麼去掉index.php
要移除URL中的"index.php",可以通過以下步驟實現:
1. 啟用「mod_rewrite」:在Apache配置文件(通常為"conf/httpd.conf")中找到"LoadMole rewrite_mole moles/mod_rewrite.so"一行,去除行首的「#」以啟用mod_rewrite模塊。
2. 編寫.htaccess規則:在Apache根目錄下創建或編輯.htaccess文件,添加以下規則:
<IfMole mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfMole>
上述規則定義了一個重寫規則,當請求匹配非目錄或非文件路徑時,將該路徑重定向到"index.php"並附帶原始路徑參數。
若在CodeIgniter根目錄下存在如css、js等子目錄,需調整.htaccess規則以正確處理這些路徑,規則可能如下:
<IfMole mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfMole>
該規則排除了"index.php"、"images"、"css"和"robots.txt"文件,確保其他路徑正確重定向至"index.php"。
C. 加了.htaccess文件為什麼仍不能去掉index.php
1. 在httpd.conf中,將 所有 AllowOverride None 改為 AllowOverride All 好像共有三處吧,然後重啟apache2. 在.htaccess文件中,是這樣的配置:RewriteEngine onRewriteCond $1 !^(index\.php|robots|stafile|upload|\.txt)RewriteRule ^(.*)$ /index.php/$1 [L]如果你是直接在根目錄配置的,則以上就可以了;假如你的項目在根目錄底下一層,則 RewriteRule ^(.*)$ /index.php/$1 [L] 得變為 RewriteRule ^(.*)$ /項目文件夾名/index.php/$1 [L]以上兩步已經可以去掉 index.php了,假如項目中的路徑,為了去掉index.php,還需要第 3 步 操作,因為第 3 步 是影響到 site_url();這個函數的。