4

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>  
3
  • That's not an URL rewriting problem, albeit you could work aorund the situation with more elaborate rules. You need to fix the resource links in your HTML. Most coders prefer absolute script and style sheet links, but you might as well use the <base href=> workaround, which exists for that very purpose. Commented Aug 16, 2012 at 1:47
  • 1
    possible duplicate of Missing CSS file and images after URL rewrite Commented Aug 16, 2012 at 1:48
  • 1
    possible duplicate of Url rewriting broke link to css Commented Aug 16, 2012 at 1:48

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

What about using the <base> tag?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.