php mvc和.htaccess url重写

问题描述:

我是一名新的程序员,目前正在学习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://*.com/questions/8216306/missing-css-file-and-images-after-url-rewrite) – mario 2012-08-16 01:48:02

+1

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

最佳对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 [ '走出去']将“测试/帕拉姆' 你需要爆炸它获得'帕拉姆'行动

请在html确保你的CSS文件绝对路径/assets/mystyle.css代替相对assets/mystyle.css../assets/mystyle.css

+0

使用''标签怎么样? – 2017-03-18 05:36:44