2011-11-22 146 views
1

我一直在嘗試從我網站上的所有頁面中刪除「.php」,同時也使用.htaccess從網址中刪除所有尾部斜線和「www」。我能夠同時使用其中的一個或兩個,但不是全部。我真的很感激任何幫助。如何刪除並重定向所有.php擴展名,重定向「www」並刪除結尾的斜槓?

例如:

  • example.com/到example.com
  • www.example.com到example.com
  • example.com/index到example.com
  • 例子。 com/index.php到example.com
  • example.com/chatroom.php to example.com/chatroom
  • example.com/directory/ to example.com/directory

我想確保舊的URL重定向到新的URL,並且我沒有任何與PHP文件共享名稱的目錄。在我的.htaccess文件中,我也有阻止特定引用者並指定403和404錯誤的代碼。 非常感謝!

回答

0

Hide PHP using htaccess URL rewriting

URL Rewriting for beginners

How to do URL re-writing in PHP?

URL Rewriting tool

檢查上面的網址。在這些教程中,您可以看到如何重寫URL。

+0

謝謝你的鏈接,但我仍然很困惑如何做重定向。他們並沒有真正展示如何在同一時間做到這一切。 – Deshar

+0

要重寫URL,應該在Web目錄的根文件夾中創建一個.htaccess文件。並且必須將以下代碼作爲您的要求。 選項+的FollowSymLinks RewriteEngine敘述 上 重寫規則^(。*)\。HTM $ $ 1.PHP [NC] 你檢查這些行。 您可以從任何網站下載.htaccess文件。 –

0

我今天對於尾部斜槓剛剛有同樣的問題。這是我怎麼會解決它在我的.htaccess文件(重要的是DirectorySlash和第一重寫規則):

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
#Do not automatically add a slash for existing directories 
DirectorySlash Off 


#No trailing slash. This means that an URL entered with trailing/will change to an URL without trailing/
#The !-d rule can be included so that any URLs to existing directories are not changed 
#RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} (.+)/$ 
RewriteRule^%1 [L,R=301] 

#Allow URL's which do not have the php file extension 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule^%{REQUEST_FILENAME}.php [L] 


#Redirect any file which does not exist to the index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /index.php [R=301,L] 

</IfModule> 

所以,如果你鍵入URL作爲其example.com/index/會重定向到example.com /指數。如果你輸入example.com/chatroom(和chatroom.php存在),它仍然在URL中有example.com/chatroom,但你的PHP腳本收到example.com/chatroom.php

你將不得不添加的是這兩種情況下重定向index.php到/並刪除www

我已包括(我不知道你是否需要它)是最後一條規則,將任何不存在的文件重定向到index.php

相關問題