2011-06-14 202 views
0

1)我想創建一個重寫規則,如果文件擴展名是PHP或(S)HTM(L),則會在URL末尾刪除文件。例如:HTACCESS重寫和重定向問題

This: http://www.example.com/index.php 
Becomes: http://www.example.com/ 

This: http://www.example.com/index.php?action=login 
Becomes: http://www.example.com/?action=login 

2)我想設置一些URL我的.htaccess重定向,這樣,如果有人瀏覽某個路徑被自動重定向到的index.php。例如:

This: http://www.example.com/classes/(index.php) 
Redirected To: http://www.example.com/(index.php) 

This: http://www.example.com/classes/class.php 
Redirected To: http://www.example.com/(index.php) 

This: http://www.example.com/classes/style.css 
Redirected To: http://www.example.com/(index.php) 

有什麼線索嗎? 非常感謝!

回答

0

對於1)你可以反轉你的問題,mod-rewrite的工作是讀髒網址並找到真實文件,所以你想要的是使用一個錯誤的URL(沒有index.php)和那個mod-重寫找到index.php。 Mod-rewrite不會修改您的網址,它是您的應用程序代碼作業(PHP?)

所以我們說你的應用程序寫這個網址:

http://www.example.com/ 

Apache會用事實

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

http://www.example.com/index.htm 

http://www.example.com/index.html 

迴應不被國防部重寫,而只是由Apache配置指令

DirectoryIndex index.php,index.htm,index.phtml 

對於http://www.example.com/?action=login它也應努力處理。對於2):好吧,目前尚不清楚。你總是想重定向試圖訪問你的類目錄的人嗎?然後兩兩件事:

  1. 如果該目錄should'nt從網絡訪問把它放在web目錄,沒有人會看到它,外面如果它僅包含PHP類PHP可以包括不在文件網頁目錄。如果它包含css和js文件,那麼這是一個錯誤,將它們重定向到根目錄中肯定不會給它們一個js或一個css
  2. 您不需要mod-rewrite來進行simpel重定向,mod_alis,它是一個模塊你肯定有(可能性99%)procides的RedirectRedirectMatch關鍵字,你可以寫:

    RedirectMatch永久/classes/.* http://www.example.com/

一如既往檢查this page:的是可以做的規則很容易不進入mod-rewrite的大腦卷積。