php mvc 和 .htaccess url 重写

标签 php apache .htaccess url-routing

我是一名新程序员,目前正在学习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/

相关文章:

php - Zend GData 上传文件到指定文件夹

java - 在 tomcat 上部署 java 应用程序

apache - 设置重定向到特定 url 的 url,无需重写

php - 如何为我的 codeigniter 网站编写 .htaccess 文件

php - 尝试创建干净的 url 时 css 消失

php - jason lewis basset 在 laravel 4 中没有很好地显示图标

php - 有没有办法在 SQL 更新语句中添加 PHP IF 语句

php - MYSQL 按日期分组 + 3 小时

apache - 为什么默认情况下Varnish sess_timeout如此之低?

mysql - MySQL Xampp 中的错误