2016-02-08 90 views
1

我對我正在處理的小型網站的.htaccess的配置有問題。.htacces url_rewrite困難

我希望將所有頁面重定向到index.php?page=REQUEST,該文件將在數據庫中查找所請求頁面的內容。

的問題時,我已經安裝了一個論壇出現,所以我想這些論壇網頁重定向到index.php?page=forum&params

Options +FollowSymlinks 

RewriteEngine on 
RewriteCond %{REQUEST_URI} /(.*).html 

RewriteRule ^(.*)forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L] 

RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L] 
RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L] 

Evetything工作正常,但論壇的一部分。我如何需要更改.htacces

+3

你必須表現出什麼論壇網址是什麼樣子。如果我們看不到輸入是什麼,我們無法修復。 –

+0

例如這個網址:www.url.com/en/forum/category/1 –

回答

0

問題似乎是您的RewriteCond匹配的請求以.html結尾。由於您的論壇網址未以.html結尾,因此後續RewriteRule的條件從未得到滿足。

還有一些其他可能出現的問題太多:

時的模樣,你可能只是想 en

  • category/(.*)將匹配任何字符,包括正斜槓和類似

    • ^(.*)forum將匹配www.url.com/en/。大概你只是希望它匹配一個十進制標識符。

    • 鏈接到您的重寫配置中未涵蓋的內容,例如圖片

    我可能會重寫你的配置看起來像這樣(在Apache的NB沒有測試,只有在正則表達式調試器):

    RewriteEngine on  
    
    # only match forum URLs 
    # e.g    url.com/en/forum/category/12345 
    RewriteCond %{REQUEST_URI} ^/.+/forum/category/[0-9]+ 
    RewriteRule ^/(.+)/forum/category/([0-9]+) index.php?page=forum&lang=$1&category=$2 [L] 
    
    # match all URLs ending in .html 
    # e.g.   url.com/en/foo.html 
    # and    url.com/foo.html 
    RewriteCond %{REQUEST_URI} ^/.+\.html$ 
    
    # a bit complicated, this matches both 
    #      /apage.html 
    #    /folder/apage.html 
    RewriteRule ^(?:/(.+))?/(.+)\.html$ index.php?lang=$1&page=$2 [L] 
    

    第二重寫規則應該始終提供一個值page,但如果URL的格式爲/lang/page.html,則僅爲lang提供值。如果你的index.php文件可以接受一個空的lang參數或者提供一個默認值,這應該是OK的。

    或者,如果您不介意保留您現有的正則表達式,並且它只是圖片,CSS等,您希望繞過URL重寫,您可以在開始時添加一些規則以跳過它們,例如

    RewriteEngine on  
    
    # don't actually rewrite, and stop processing rules 
    RewriteRule \.(jpg|png|css|js)$ - [L] 
    
    # only match forum URLs 
    # e.g    url.com/en/forum/category/12345 
    RewriteCond %{REQUEST_URI} ^/.+/forum/category/[0-9]+ 
    RewriteRule ^/(.+)/forum/category/([0-9]+) index.php?page=forum&lang=$1&category=$2 [L] 
    
    etc... 
    
  • +0

    感謝hilsy,我已經刪除它,現在它更好,但現在我沒有看到網站上的圖像。這是爲什麼 ? –

    +0

    我更新了我的答案以解決這個問題,並且還增加了我可能(重新)寫正則表達式的時間,使其更加簡短和清晰。 – hillsy

    +0

    嗨希爾,不幸的是你的配置建議不適合我。除了我在論壇頁面(f.e. url.com/en/forum/category/1)上的圖片之外,您的第一個答案+圖片的添加內容除外,不會顯示圖片。請注意,我使用圖像的相對路徑。 –

    0
    RewriteEngine on 
    
    RewriteRule \.(jpg|png|gif|svg|css|js)$ - [L] 
    
    RewriteRule ^(.*)/forum/topic/(.*)?$ index\.php?page=forum&lang=$1&topic=$2 [L] 
    RewriteRule ^(.*)/forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L] 
    
    RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L] 
    RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L]