2011-11-28 28 views
0

我怎樣才能永遠不會顯示index.php?例如,爲了
site.com/index.php/controller所有請求重定向在瀏覽器地址欄
site.com/controller使用.htaccess,從不顯示index.php

我當前htaccess的去除的index.php但是當用戶直接類型site.com/index.php/controller它們仍然顯示在地址欄中該地址,而不是site.com/controller

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

注:燃燒之前,我已經檢查了許多能夠解決重定向的index.php的.htaccess的線程,但我還沒有找到一個從來沒有的index.php顯示。這裏有幾個...
Not Show index.php in subdirectory
remove index.php in codeigniter

+0

任何人都能夠在此闡明?任何幫助深表感謝。謝謝。 – csi

回答

0

嘗試添加以下到.htaccess文件在你的域

RewriteEngine On 
RewriteBase/

#if you get a request for this /index.php/controller 
RewriteCond %{REQUEST_URI} ^/index.php/controller$ 
#redirect to just controller 
RewriteRule . controller  [R=301,L] 

的根。如果您需要這適用於任何PATH_INFO使用下面的規則,而不是

RewriteEngine On 
RewriteBase/

#if you get a request for this /index.php(any-path) 
RewriteCond %{REQUEST_URI} ^/index.php/(.+)$ 
#redirect to any-path 
RewriteRule . %1  [R=301,L] 
+0

我認爲'''site.com/index.php/controller'''只是一個例子......他肯定需要佔位符。 –

+0

@ alessandro1997確定,更新後可以使用任何路徑信息 –

+0

我試過這個,但沒有運氣。此外,我正在與Codeigniter框架合作,但我不相信這會有所作爲。謝謝,但任何其他的想法?是的佔位符。 – csi

0

嘗試:

RewriteEngine On 
RewriteBase/

RewriteRule ^index.php(.*)% ^/$1 [QSA,R] 

我還沒有測試過這個,但據我瞭解,它將採用一個帶有index.php作爲文件名的URL(不管接下來的任何內容)並將其重定向到/(.*)(即。任何在原始請求中遵循index.php的內容)。 QSA標誌追加查詢字符串,R使它成爲一個硬重定向,而不是僅僅重寫當前請求的URL。

0

爲尋找一個更廣義的解決方案:

Options +FollowSymLinks -MultiViews 

#Disallow listing contents of subfolders 
Options All -Indexes 

# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

# If index or index.php requested, strip and redirect 
RewriteCond %{THE_REQUEST} index(\\.php)? 
RewriteRule ^index(\\.php)?$ http://yourdomain.com/ [R=301,L] 

## Part 1 
## hide .php extension 
# To externally redirect /dir/foo.php to /dir/foo 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] 
RewriteRule^%1 [R,L,NC] 

## Part 2 - handle incoming that lacks extension (except for existing folders) 
## To redirect /dir/foo to /dir/foo.php, (skip for admin or user dirs) 
RewriteRule ^(admin|user)($|/) - [L] 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule^%{REQUEST_URI}.php [L] 
相關問題