2010-12-06 159 views
32

我有一個問題,谷歌已經索引了一些頁面的URL錯誤。htaccess從url中刪除index.php

的URL他們索引是:

http://www.example.com/index.php/section1/section2 

我需要它重定向到:

http://www.example.com/section1/section2 

的.htaccess是不是我的專長,所以任何幫助,將不勝感激。

在此先感謝。

回答

43

這是從URL中隱藏index.php的基本規則。把這個放在你的根目錄.htaccess文件中。

mod_rewrite必須啓用PHP,這將適用於高於5.2.6的PHP版本。

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

非常感謝,還有一個問題 - 我如何替換下劃線的連字符,即 http://www.mydomain.com/section-1/section-2 至 http://www.mydomain.com/section_1/section_2 – Nick 2010-12-06 10:04:54

+6

如果您真的在爲SEO工作,無需將連字符轉換爲下劃線。人們正在做SEO的逆向。連字符是谷歌友好。 – 2010-12-06 10:12:08

+13

這不會刪除index.php,而是添加它! – Calmarius 2014-05-06 08:17:24

15
RewriteEngine On 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?) 
RewriteRule^/%1 [R=301,L] 
28

從URL中移除index.php,以及訪問者重定向到非index.php文件版本的頁面:

RewriteBase/
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] 
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L] 

這將乾淨重定向/index.php/myblog簡單/myblog

使用301重定向將保留Google搜索引擎排名。

4

執行以下步驟

確保主機/你的電腦的mod_rewrite模塊處於活動狀態。如果沒有激活,然後嘗試以某種方式激活,請打開httpd.conf文件。你可以在phpinfo.php中查看它以找出答案。

改變這個設置:

#LoadModule rewrite_module modules/mod_rewrite.so 

是,重啓WAMP

LoadModule rewrite_module modules/mod_rewrite.so 

2.然後去.htaccess文件,並試圖修改爲:

RewriteEngine On 

RewriteBase/

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA] 

如果上面不行,試試這個:

RewriteEngine on 

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# otherwise forward it to index.php 
RewriteRule . index.php 

移動.htaccess文件根目錄,其中是的index.php那裏。

www OR root folder 
- index.php 
- .htaccess 
3

假設存在的網址是

http://example.com/index.php/foo/bar 

,我們希望將其轉換成

http://example.com/foo/bar 

您可以使用以下規則:

RewriteEngine on 
#1) redirect the client from "/index.php/foo/bar" to "/foo/bar" 
RewriteCond %{THE_REQUEST} /index\.php/(.+)\sHTTP [NC] 
RewriteRule^/%1 [NE,L,R] 
#2)internally map "/foo/bar" to "/index.php/foo/bar" 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ /index.php/$1 [L] 

在SPEP #1我們首先匹配請求字符串並捕獲/index.php/後的所有內容,並將捕獲的值保存在%1 var。然後,我們將瀏覽器發送到一個新的URL。 #2在內部處理請求。當瀏覽器到達/foo/bar時,#2rule會將新網址重寫爲原始位置。