2013-08-17 51 views
0

我試圖從.htaccess的URL中刪除index.php,我已經到了一半,我無法弄清楚什麼是錯的。如何從URL中刪除index.php?

我/使用以下設置/系統:

  • 笨2.1.4
  • Ubuntu的12.04
  • $config['base_url'] = 'http://localhost/present';
  • 的apache2
  • PHP5

當I」請嘗試以下網址:

http://localhost/present/test它顯示頁面。到現在爲止還挺好。

http://localhost/present/index.php/test它也顯示頁面,所以我得到重複的頁面。這是我的問題。

我在與index.php相同的文件夾中使用以下.htaccess。

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* index.php/$0 [PT,L] 

一些友善的靈魂能告訴我什麼是錯的嗎?它是mod_rewrite還是一些服務器/ php設置是錯誤的?

+0

你對你希望你的htaccess做什麼有點含糊。 –

+0

我想要htaccess從url中刪除index.php。例如下面的原始URL請求localhost/present/index.php/test將被改爲localhost/present/test –

回答

-1

試試這個:

DirectoryIndex index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule^index.php [L] 

我使用它來重定向所有請求到index.php

+0

這並不回答這個問題...... – Sumurai8

+0

對於提示而言,但那不起作用爲了我。 –

0

爲了讓花哨的URL,你需要一個內部重寫(你有)和外部重定向(你沒有)。困難的部分不是無限循環。

如果您使用的是Apache 2.3.9或更高版本,則可以使用END標誌。

#Redirect 
RewriteRule ^index\.php/(.*)$ $1 [R,L] 

#Internal Rewrite 
RewriteCond %{REQUEST_URI} !^/index\.php 
RewriteRule ^(.*)$ index.php/$0 [END] 

如果您使用的是較低版本,則需要使用hack-ish解決方法。 THE_REQUEST只會在實際請求中設置,而不是重寫。

#Redirect 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/index\.php [NC] 
RewriteRule ^index\.php/(.*)$ $1 [R,L] 

#Internal Rewrite 
RewriteRule ^(.*)$ index.php/$0 [L] 
+0

我使用的是Apache2 2.2.22-1ubuntu1.4,你的例子根本不工作。現在'localhost/present/test'將不起作用。 獲取以下錯誤消息(在此服務器上未找到請求的URL /目前/測試。) –

+0

您可能需要在部分零件上加上「present /」 – Sumurai8

+0

我沒有時間修改或測試這(必須離開家吃晚飯),但我認爲你應該設置RewriteBase爲/ present – Sumurai8

0

這應該有效。

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} !no-redir 
RewriteRule ^/?index.php/(.*)$ $1 [R=301,NC,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !index\.php 
RewriteRule .* index.php/$0?no-redir [L] 
+0

對不起,但它不會工作。獲取以下錯誤消息:」請求的U​​RL /目前/測試未在此服務器上找到「 –

+0

您的htaccess位於何處?根內部? –

+0

是的,在第一個文件夾」 /「與index.php文件。 –