我是一名新程序员,目前正在学习php mvc。我正在从头开始构建框架/mvc 系统...现在我面临 .htaccess 的问题。
example.com = localhost // unable to post question for localhost link
我的申请网址是这样的...
http://example.com/mvc/index.php?go=test //test is a controller's name
By the help from .htaccess I cleaned the url to....
http://example.com/mvc/test
If a controller does not exists it shows an error view, which contains some error message.
http://example.com/mvc/doesnotexists
But if I put "/" (forward slash) in the url... http://example.com/mvc/test/
it shows the error but does not load any css or js file.
I have also included controllers method in url...
http://example.com/mvc/test/viewresult
If no matched method found in that controller show error message, it shows but same problem...no css nor js.
I have fully loaded css and js if I go through full url
http://example.com/mvc/index.php?go=doesnotexists/
and so on
I have tried several .htaccess rules but unable to get it work. Please help and thanks in advance.. My currrent .htaccess code...
<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>
最佳答案
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
如果你的'test'是你的 Controller ,而'param'是'test' Controller 的 Action 你可以使用
http://example.com/mvc/test/param
$_GET['go'] 将是 'test/param' 你需要爆炸它来获得'param' Action
关于php mvc 和 .htaccess url 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11979579/