2011-07-20 93 views
0

我想我的網址更改爲:htaccess的清潔網址

www.xxxx.com/en/ -> index.php?lang=en 
www.xxxx.com/news -> index.php?mod=news 

我用這個代碼,但它不工作:

RewriteCond %{REQUEST_URI} ^/(pe|en|fr|sp|ar)/$ 
RewriteRule ^([^/]*)/$ index.php?lang=$1 

RewriteCond %{REQUEST_URI} !^/(pe|en|fr|sp|ar)$ 
RewriteRule ^([^/]*)$ index.php?mod=$1 

var_dump($_GET)結果:

array(2) { ["mod"]=> string(9) "index.php" ["PHPSESSID"]=> string(32) "e7a5fc683653b7eea47a52dfc64cd687" } 

我也使用htaccess測試儀(http://htaccess.madewithlove.be/),所有的東西都可以! :(

回答

1

兩個規則通過,並因此被施加,第一重寫/en//index.php?lang=en。然後第二規則通過,並重寫到/index.php?mod=index.php

使用[L]選項來停止處理一次給定的規則經過:

RewriteCond %{REQUEST_URI} ^/(pe|en|fr|sp|ar)/$ 
RewriteRule ^([^/]*)/$ index.php?lang=$1 [L] 

RewriteCond %{REQUEST_URI} !^/(pe|en|fr|sp|ar)$ 
RewriteRule ^([^/]*)$ index.php?mod=$1 [L] 
+0

沒有奏效它再回到前面的結果:( –

+0

@Hamid:!真的嗎?我跑了通過您所提供的htaccess的測試儀,和得到了預期的結果 – Matchu

+0

它在htaccess測試儀上工作,但它在我的服務器上不起作用!:( –

1

你捕捉的index.php忽略現有的文件和目錄,並停止處理規則與[L]

RewriteCond %{REQUEST_URI} ^/(pe|en|fr|sp|ar)/$ 
RewriteRule ^([^/]*)/$ index.php?lang=$1 [L] 

# Don't rewrite index.php or other existing file/dir 
RewriteCond %{REQUEST_URI} !-f 
RewriteCond %{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} !^/(pe|en|fr|sp|ar)$ 
RewriteRule ^([^/]*)$ index.php?mod=$1 [L] 
+0

它沒有工作!它再次返回以前的結果:(! –

0

這爲我一個人工作得很好:

RewriteRule ^(pe|en|fr|sp|ar)/$ index.php?lang=$1 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(?!(?:pe|en|fr|sp|ar)/)([^/]*)$ index.php?mod=$1 [L] 
  1. 你絕對必須使用[L]標誌在這種情況下

  2. 我得到了第一條規則擺脫RewriteCond - 花紋足夠簡單並且不需要單獨的條件(這意味着在我的實現中必須完成2個匹配而不是1個)。

  3. 在第二個規則中,使用%{REQUEST_FILENAME}來檢查請求的資源(原始文件或已被重寫)是否在真實文件/文件夾中,然後纔會重寫。這將防止你面臨的雙重改寫。

  4. 我也擺脫了RewriteCond這裏的規則是不是太複雜(它更難以閱讀,特別是如果你正則表達式的技能是不是很大,但它的工作)。這也使得我的回答已經提供了不同的一點:)