2012-08-16 140 views
4

我是一名新的程序員,目前正在學習php mvc。我從頭開始構建一個框架/ mvc系統...現在我面臨着.htaccess的問題。php mvc和.htaccess url重寫

example.com = localhost // unable to post question for localhost link 

我的應用程序的URL是這樣的...

  http://example.com/mvc/index.php?go=test //test is a controller's name

通過從.htaccess中的幫助下,我清理了URL來....

  http://example.com/mvc/test

如果控制器不存在它顯示錯誤視圖,其中包含一些錯誤消息。

  http://example.com/mvc/doesnotexists
但是,如果我把「/」(正斜槓)在url ...
  http://example.com/mvc/test/
它顯示錯誤,但不會加載任何css或js文件。

我也包括控制器方法在URL ...

  http://example.com/mvc/test/viewresult
如果在該控制器中找不到匹配的方法顯示錯誤消息,它顯示,但同樣的問題...沒有CSS也沒有js。

我已經完全加載CSS和JS如果我經歷完整的URL

  http://example.com/mvc/index.php?go=doesnotexists/

我嘗試了好幾種的.htaccess規則,但無法得到它的工作。請幫忙在此先感謝。我currrent的.htaccess代碼...

<IfModule mod_rewrite.c> 

    # Turn on 
    RewriteEngine On 

    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-l 

    #RewriteCond %{REQUEST_URI} !^/assets/ 
    #RewriteCond $1 !^(favicon\.ico|favicon\.png|media|robots\.txt|crossdomain\.xml|.*\.css|.*\js) 

    RewriteRule ^(.+)$ index.php?go=$1 [QSA,L] 

    # Generic 404 for anyplace on the site 
    # ... 

</IfModule> 
+0

這不是一個URL重寫問題,雖然你可以通過更詳盡的規則工作aorund情況。您需要修復HTML中的資源鏈接。大多數編碼人員更喜歡絕對的腳本和樣式錶鏈接,但您也可以使用''解決方法,該解決方法就是爲此目的而存在的。 – mario 2012-08-16 01:47:02

+1

[URL重寫後丟失CSS文件和圖像]的可能重複(http://stackoverflow.com/questions/8216306/missing-css-file-and-images-after-url-rewrite) – mario 2012-08-16 01:48:02

+1

[Url重寫打破鏈接到CSS](http://stackoverflow.com/questions/5462897/url-rewriting-broke-link-to-css) – mario 2012-08-16 01:48:55

回答

5

最佳對CSS和JS的解決方案是使用完整路徑:

http://example.com/mvc/assets/style.css 

http://cdn.example.com/style.css 

和重寫

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*) index.php?go=$1 

,如果你的 '測試' 是你的控制器,以及 'PARAM' 是 '測試' 控制器的作用 可以使用

http://example.com/mvc/test/param 

的$ _GET [ '走出去']將「測試/帕拉姆' 你需要爆炸它獲得'帕拉姆'行動

0

請在html確保你的CSS文件絕對路徑/assets/mystyle.css代替相對assets/mystyle.css../assets/mystyle.css

+0

使用''標籤怎麼樣? – 2017-03-18 05:36:44