2013-10-28 85 views
0

我都與我的重寫感到困惑。我要做到以下幾點:使用.htaccess刪除部分網址。 404重寫後

  • example.com/ 不/需要的index.php→example.com/index.php
  • example.com/ 不/需要 /文件夾/→ example.com/folder/
  • 查詢參數應保留

我已經有一個重寫規則,沒有工作:

Options +FollowSymlinks -MultiViews 

RewriteEngine On 
RewriteBase/

RewriteRule ^not/needed(.*)$ $1 [L,R=302,QSA] 

但是在重寫之後,我得到了404。我怎麼能告訴服務器它應該顯示來自原始URI的內容?

回答

0

你的規則只適用於一個方向。 RewriteRule <target> <destination>,沒有什麼魔法可以讓它逆轉。您需要明確創建一個相反的規則:

Options +FollowSymlinks -MultiViews 

RewriteEngine On 
RewriteBase/

RewriteCond %{THE_REQUEST} \ /+not/needed/(.*) 
RewriteRule ^not/needed(.*)$ $1 [L,R=302] 

RewriteCond %{REQUEST_URI} !^/not/needed/ 
RewriteRule ^(.*)$ /not/needed/$1 [L]