2013-03-07 102 views
0

這已被要求分配在互聯網上,這裏在stackoverflow。 但我不能得到它的工作。 (使用.htacces第一次) 什麼即時試圖做的是一些簡單的東西:.htaccess重寫不工作

  1. 從URL中移除www
  2. 從url中刪除.php
  3. 從主頁刪除/index如果鏈接回它。

根據我的網站主辦公司,這是他們所使用的:
的Apache 2.2+和PHP 5.3+ - MySQL的5.1+

的.htacces文件位於根文件夾的index.php 我已嘗試分配的東西,但這是我現在得到的:

RewriteEngine On 
#www to non www 
RewriteCond %{HTTP_HOST} !^mysite.se$ [NC] 
RewriteRule ^(.*)$ http://mysite.se/$1 [L,R=301] 

#remove .php 
RewriteCond %{HTTP_HOST} ^mysite\.se$ [NC] 
RewriteCond %{QUERY_STRING} !internal=1 [NC] 
RewriteRule ^(.*)\.php$ $1 [L,R=301] 

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

先感謝您的任何幫助,您可以提供。

+0

重寫日誌說什麼? – Waygood 2013-03-07 13:34:58

+0

「不工作」是什麼意思? – 2013-03-07 13:36:20

+1

你還沒有說過這三個中的哪一個不適合你,反而會發生什麼。 – hakre 2013-03-07 13:36:21

回答

1

您以錯誤的方式使用.htaccess

不得使用htaccess的生成一個友好的URL,您必須使用它來一個友好的URL轉化爲真正的

RewriteEngine On 
RewriteBase/

#redirect www.mysite.se to mysite.se 
RewriteCond %{HTTP_HOST} !^mysite.se$ [NC] 
RewriteRule ^(.*)$ http://mysite.se/$1 [L,R=301] 

#redirect mysite.se/page to mysite.se/page.php (transparent) 
#as this rule is extremely permissive, we desactivate it when we're calling 
#a real file, to avoid this kind of situation : mysite.se/img/mylogo.jpg.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ $1.php [L] 

#redirect mysite.se/index to mysite.se/ (visible) 
RewriteRule ^index$/[L,R=301] 

在你的代碼,使用友好的URL,和讓htaccess重定向它。

如果你不使用這種方法,你就會陷入一個自我毀滅的模式,試圖page.php重定向到page(獲得不錯的網址),並重定向到pagepage.php(因爲你的應用程序只知道page.php) 。

+0

所以我應該寫沒有.php我的網址? – objectnobb 2013-03-07 14:06:29

+0

是的,無處不在使用.php的URL。 – zessx 2013-03-07 14:07:55

+0

多數民衆贊成在不方便:( – objectnobb 2013-03-07 14:14:23