2016-12-18 63 views
1

我使用.htaccess獲得更好的URL。使用.htaccess重寫問題

我有這個PHP頁面:

治療,line.php

隨着

#rewrite treatments-line 
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L] 

我能順利進入頁面/處理/線/ the_id_of_the_page。

但是當我創建一個頁面處理.php時,重寫處理行不起作用,它只是顯示首頁。

對於測試時,我改變方法改寫成這樣:

#rewrite treatments-line 
RewriteRule ^/?treatment/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L] 

我剛剛更名爲治療(「S」),上/治療/線路重寫/ the_id_of_the_page工作正常,但我不想更改名稱。

所以,我可以看到與名稱有衝突,因爲treatments.php文件。

我該如何解決這個問題?

這兩個PHP頁面位於網站的根目錄中。

EDIT2:

這是我整個的.htaccess代碼:

Options -MultiViews 
RewriteEngine on 

#remove .php 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

RewriteCond %{REQUEST_URL} /(.*)$ 
RewriteRule ^(.*)$ %1.php [NC,L] 

#redirect posts-view 
RewriteRule ^/?post/([0-9]+)$ posts-view.php?id=$1 [NC,L] 

#rewrite products-zone 
RewriteRule ^/?products/zone/([0-9]+)$ products-zone.php?id=$1 [NC,L] 

#rewrite products-category 
RewriteRule ^/?products/category/([0-9]+)$ products-category.php?id=$1 [NC,L] 

#rewrite products-solution 
RewriteRule ^/?products/solution/([0-9]+)$ products-solution.php?id=$1 [NC,L] 

#rewrite products-line 
RewriteRule ^/?products/line/([0-9]+)$ products-line.php?id=$1 [NC,L] 

#rewrite treatments-line 
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]  

#rewrite treatments-category 
RewriteRule ^/?treatments/category/([0-9]+)$ treatments-category.php?id=$1 [NC,L] 

#rewrite product-view 
RewriteRule ^/?products/([0-9]+)$ product-view.php?id=$1 [NC,L] 

#redirect lang 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.+?)/(.*)$ $2.php?lang=$1 [L] 

#RewriteRule ^([^\.]+)/?$ index.php?lang=$1 [L] 
+0

你叫兩個文件'治療,line.php'和'treatments.php'和你想有兩個相同的重寫URL模式? – Perumal

+0

不,這兩個頁面有不同的內容。我只想重寫URL模式治療line.php。 – user2519032

+0

是的。這是因爲有'treatments.php'。 – Perumal

回答

2
Options -MultiViews 
RewriteEngine On 
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L] 

添加Options -MultiViews解決你的問題。

更多有關Options -MultiViews

一個多視圖搜索由多視圖選項啓用。如果 服務器收到/ some/dir/foo的請求並且/ some/dir/foo不存在 ,則服務器讀取目錄以查找名爲 foo。*的所有文件,並有效地僞造一個類型映射將所有這些文件命名爲 文件,爲它們分配相同的媒體類型和內容編碼 如果客戶要求其中一個名稱。然後 選擇最符合客戶要求的,並返回 文件。

http://httpd.apache.org/docs/2.2/en/mod/mod_negotiation.html#multiviews

根據你的.htaccess代碼,也是評論,您需要將#remove .php規則的位置改變到的.htaccess的盡頭。所以,這是你的.htaccess代碼應該是怎麼樣的:

Options -MultiViews 
RewriteEngine on 

#redirect posts-view 
RewriteRule ^/?post/([0-9]+)$ posts-view.php?id=$1 [NC,L] 

#rewrite products-zone 
RewriteRule ^/?products/zone/([0-9]+)$ products-zone.php?id=$1 [NC,L] 

#rewrite products-category 
RewriteRule ^/?products/category/([0-9]+)$ products-category.php?id=$1 [NC,L] 

#rewrite products-solution 
RewriteRule ^/?products/solution/([0-9]+)$ products-solution.php?id=$1 [NC,L] 

#rewrite products-line 
RewriteRule ^/?products/line/([0-9]+)$ products-line.php?id=$1 [NC,L] 

#rewrite treatments-line 
RewriteRule ^/?treatments/line/([0-9]+)$ treatments-line.php?id=$1 [NC,L]  

#rewrite treatments-category 
RewriteRule ^/?treatments/category/([0-9]+)$ treatments-category.php?id=$1 [NC,L] 

#rewrite product-view 
RewriteRule ^/?products/([0-9]+)$ product-view.php?id=$1 [NC,L] 

#redirect lang 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.+?)/(.*)$ $2.php?lang=$1 [L] 

#RewriteRule ^([^\.]+)/?$ index.php?lang=$1 [L] 

#remove .php 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^([^\.]+)$ $1.php [NC,L] 
+0

我已經添加了'Options -MultiViews',但它仍然不起作用,即使我已經清除了緩存瀏覽器。如果我把treatments.php重命名爲treatment.php,那麼重寫就是有效的。 – user2519032

+0

我最初有同樣的問題。我試圖通過像「http:// localhost/treatments/line/2」這樣的URL,然後運行。最初它是'http:// localhost/treatments/line/1'。即使當我清除緩存並打開新標籤並傳遞相同的URL時,這一項也不起作用。最後我將URL中的數字更改爲2,並且工作正常。我不確定是什麼造成了這樣的問題。也許清除緩存或關閉整個瀏覽器本身可以解決問題。 – Perumal

+0

我也嘗試過不同的數字,但仍然沒有運氣。 – user2519032