2009-05-25 177 views
3

我使用的是Kohana的框架(但我認爲這是不相關這一問題)和頁面可以像這樣從網站網址

http://www.example.com/articles/ 
http://www.example.com/index.php/articles/ 

訪問現在,我一般經驗法則刪除的index.php嘗試和調整我的.htaccess只允許一個頁面的方式,並默默地重定向其他常見的方式。

本質上,在上面的第一個URL中,地址實際上是在內部重定向到第二個示例。

我想要做的是強制任何第二種類型的URL變成第一種類型的URL。我不是在.htaccess經常有信心,我的第一次嘗試是投擲一些意想不到的結果(比如有時無限循環)

這裏是我想出了

RewriteRule ^index\.php/(.*) $1 [NC,L,R=301] 

誰能告訴我我做錯了什麼,如果你也遇到過這個問題,你怎麼解決它?

編輯

我決定後我整個的.htaccess所以我所有的重定向可以檢查。

# Turn on URL rewriting 
RewriteEngine On 

# Installation directory 
RewriteBase /~toberua/ 


# file not found page 
    ErrorDocument 404 /404/ 
    ErrorDocument 403 /403/ 

# get people out of my directories 
    Options -Indexes 

# default page to load 
    DirectoryIndex index.php 

# add trailing slash if missing 
    RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L] 

# redirect /favicon.ico requests 
    RewriteCond %{REQUEST_URI} !^/images/layout/favicon\.ico [NC] 
    RewriteCond %{REQUEST_URI} favicon\.(gif|ico|png|jpe?g) [NC] 
    RewriteRule (.*) images/layout/favicon.ico [R=301,L] 

# send /home back to TLD 
    RewriteRule home/ $1 [NC,R=301,L] 

# ensure there is no /index.php in the address bar 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/ 
    RewriteRule ^(.*)index\.php$ $1 [R=301,L] # this was my attempt to stop /dir/index.php and make it simply /dir/ 

    RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301] 

# Protect application and system files from being viewed 
RewriteRule ^(application|modules|system) - [F,L] 

# Allow any files or directories that exist to be displayed directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Rewrite all other URLs to index.php/URL 
RewriteRule .* index.php/$0 [PT,L] 

回答

3

試試這個:

RewriteRule ^index\.php/(.*) $1 [NS,NC,L,R=301] 

什麼你不處理,是所有請求都受到重寫,當你做一個重寫它​​生成一個子請求 - 這也是受重寫。因此,您將重寫/文章發佈到/index.php/articles,然後在該問題的子請求中,您將/index.php/articles改寫爲/ articles,併爲301重定向生成新的請求。添加NS標誌將使這個規則不能在子請求上運行,我認爲這應該解決你的問題,除非你也在/ articles - > /index.php/articles改寫上做了301(但那會是瘋狂的)。

+0

NS做什麼混沌? – alex 2009-05-25 01:33:43