Php mvc and .htaccess url rewrite

I am a new programmer, currently learning php mvc. I am creating a framework / mvc system from scratch ... Now I am facing a problem with .htaccess.

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

My app url is as follows:

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

Using .htaccess I cleared the url ....

  http://example.com/mvc/test 

If the controller does not exist, it displays an error message containing some error message.

 http://example.com/mvc/doesnotexists 
But if I put "/" (slash) in url ...
 http://example.com/mvc/test/ 
, it shows an error, but does not load the css or js file.

I also included the controller method in url ...

 http://example.com/mvc/test/viewresult 
If no matching method is found in this controller, an error message is displayed, but the same problem ... no css or js.

I have fully loaded css and js if I look through the full url

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

I tried a few .htaccess rules but couldn't get it to work. Please help and thanks in advance. My .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> 
+4
source share
2 answers

The best solution for css and js is to use the full path:

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

and rewrite

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

if your "test" is your controller, and "param" is the action of the "test" controller, you can use

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

$ _GET ['go'] will be 'test / param' you need to detonate it to get the action "param"

+5
source

Make sure your html CSS files are absolute paths /assets/mystyle.css instead of relative assets/mystyle.css or ../assets/mystyle.css

0
source

All Articles