2011-04-27 43 views
0

我敢肯定,這是簡單的,但我完全無法得到它的工作:\ 我試圖改寫mod_write重寫規則

http://www.example.com/products/ABC123

http://www.example.com/index.php?page=shop&code=ABC123

我已經試過

RewriteBase/

RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1 

其中re指示OK,但所有圖像(和JS CSS等)有錯誤的路徑(example.com/images/ABC123.jpg是example.com/products/images/ABC123.jpg)。此外所有的鏈接現在都走錯了路(example.com/?page=shop &文件夾= 7是example.com/products/?page=shop &文件夾= 7)

我的.htaccess文件是在根,我也試過RewriteBase產品/和刪除產品/從規則正則表達式,但只是拋出一個404

我已經讀過幾次關於mod_rewrite的官方文檔在過去一個小時,我必須錯過什麼?

**編輯:** 對不起所有的一個技巧問題,事實證明,你可以用html BASE元素來解決這個問題,我的正則表達式在開始時沒有任何問題!

我把< base在HEAD部分中,它現在都很好,很漂亮!

感謝誰教我如何設置條件,天氣的傢伙或者沒有文件/目錄是否存在

+0

順便說一句,我還發現有人暗示我放的RewriteCond \(JS | CSS | PNG | JPG)$,這似乎是一個有效的正則表達式! ,但這使得服務器拋出500錯誤:S – Pez 2011-04-27 13:43:33

回答

0
 RewriteEngine On 
     RewriteBase/

     # Generic requests for the application 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^products/(.*)$ index.php?page=shop&code=$1 [L] 

那做呢?

編輯:啊,你的頁面內鏈接錯誤是你應該在PHP中修復的。你可以試試這個:

RewriteEngine On 
    RewriteBase/

    # Generic requests for the application 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 

    # Main page 
    RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1 [L] 
    RewriteRule ^products/?(.*)$ index.php?$1 [L] 

    # Resources 
    RewriteRule ^products/images/(.*)$ images/$1 [L] 
    RewriteRule ^products/css/(.*)$ css/$1 [L] 
    RewriteRule ^products/js/(.*)$ js/$1 [L] 

不過說真的,所以他們工作的權利,你應該生成你在PHP的網址:將不必通過mod_rewrite的每次運行它們更高效。

+0

nope同樣的問題對不起,產品頁面沒有CSS,JS或圖像 – Pez 2011-04-27 14:07:40

+0

真棒謝謝這沒有直接幫助,但我有想法查找相對URL重寫,這導致我到html BASE元素,我想這就是我正在尋找 – Pez 2011-04-27 15:04:14

1

試試這個規則.htaccess文件:

Options +FollowSymlinks -MultiViews 
RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^products/([-a-z0-9]+)$ /index.php?page=shop&code=$1 [L,NC,QSA] 
+0

對不起,這似乎沒有任何影響 – Pez 2011-04-27 14:10:07

+0

請你給一些細節請像什麼不工作?即使使用上述規則,你的圖像/ css文件是否仍然重定向?我可以知道你的圖像/ css文件的絕對路徑嗎?另外你的網絡服務器的訪問日誌也可以幫助我給你更好的答案。 – anubhava 2011-04-27 14:32:44

+0

也許大小寫不敏感對變量有一些影響?但是重寫是好的。 – 2011-04-27 14:58:08