2012-03-19 71 views
3

我希望用戶能夠使用$_SERVER['PATH_INFO']作爲一個佔位符的文件服務,並在地址欄htaccess的重定向刪除的index.php

比如我想爲me.com/index.php/settings1me.com/settings1擺脫的index.php對於用戶轉到的任何PATH_INFO等等。

我該如何將它寫成htaccess規則?我甚至不知道從哪裏開始

如果有幫助,我參與了hostgator的共享託管計劃。

基於@EddyFreddy的建議,我試了兩種方式提到的問題,沒有運氣。下面是該文件的樣子至今:

suPHP_ConfigPath /home/user/php.ini 
    <Files php.ini> 
     order allow,deny  
     deny from all 
    </Files> 

RewriteEngine On 
RewriteBase/ 

RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC] 
RewriteRule .? http://mysite.com%{REQUEST_URI} [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 [L] # tried with and without the `?` 
+3

看到這個[問題] [1] [1]:http://stackoverflow.com/questions/4365129/htaccess-remove-index-php-from-url – 2012-03-19 03:58:54

+0

@EddyFreddy:它不工作,請參閱編輯 – qwertymk 2012-03-19 10:55:07

+0

@EddyFreddy:經過進一步檢查,我發現我不需要*在地址欄中有'index.php',但我也希望它在那裏被刪除。例如'me.com/settings1'可以工作,但我也希望將'me.com/index.php/settings1'重定向到'me.com/settings1' – qwertymk 2012-03-19 11:02:31

回答

8

這可以很容易地通過mod_rewrite處理。在確認一切正常,然後更改RR=301

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

# external redirect from /index.php/foo to /foo/ 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC] 
RewriteRule^%1/ [L,R] 

# external redirect to add trailing slash 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+((?!.+/[\s\?])[^\s\?]+) [NC] 
RewriteRule^%1/ [L,R] 

# internal forward from /foo to /index.php/foo 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule^index.php%{REQUEST_URI} [L,NC] 

:下DOCUMENT_ROOT使用在.htaccess這個代碼。

+0

對我來說這是有效的,但'http:// localhost/index.php'沒有重定向到'http:// localhost /'。 – 2012-03-20 13:34:01

+0

@EddyFreddy:我不確定OP是否想重定向'http:// localhost/index.php',但是現在我已經做了一個編輯來處理這個邊界情況。 – anubhava 2012-03-20 13:39:02

+0

令人驚歎的是,我總是想知道可以用神祕表達方式完成的瘋狂事情:-) – 2012-03-20 14:17:23

1

長期評估之後我發現,從anubhava版本並非在所有情況下,爲我工作。所以我嘗試了這個修改版本。它將正確處理現有dirs中的現有文件,並且不會產生雙斜槓。

Options +FollowSymLinks -MultiViews 
# Turn mod_rewrite on 
RewriteEngine On 
RewriteBase/

###### Add trailing slash (optional) ###### 
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$ 
RewriteRule ^(.*)$ $1/ [R=301,L] 

###### external redirect from /index.php/foo to /foo ###### 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/.+)?[\s\?] [NC] 
RewriteRule^%1 [L,R=301] 

###### internal forward from /foo to /index.php/foo ###### 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule^index.php%{REQUEST_URI} [L] 
+0

我應該關心的是,人們可能會鏈接到index.php它會攪亂我的排名? – qwertymk 2012-03-19 21:23:23

+0

我會忽略'index.php/settings1'重定向到'/ settings1' - 這隻有在你想將舊的PageRank解救到新的URL時纔有意義。這似乎相當困難。 也許你應該看看不同的OSS,看看他們是如何做到的。 – 2012-03-20 03:34:08

+0

@qwertymk您是否使用類似codeignitter或zend的框架,或者您是否想構建自己的解決方案? – 2012-03-20 11:58:22

相關問題