Shakil Ahmed
Shakil Ahmed

Reputation: 1521

php mvc and .htaccess url rewrite

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

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

My application url is like this...

        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>  

Upvotes: 4

Views: 7486

Answers (2)

Louis.CLast
Louis.CLast

Reputation: 454

Best solution for css and js is use 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 action of 'test' controller you can use

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

The $_GET['go'] will be 'test/param' you need explode it to get 'param' action

Upvotes: 5

Moak
Moak

Reputation: 12865

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

Upvotes: 0

Related Questions