SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mod Rewrite breaks CSS

    I just added a mod rewrite to eliminate the index.php from my URL. After doing so, my page loads except for the CSS. I can't even directly access the CSS directory (http://mysite.com/css/style.css). Without the rules in .htaccess if I went to that link I would see the plain text of my CSS file in the browser. With the rules I have listed below in .htaccess I can't access the css file (get a 404 error).

    I have read all kinds of solutions saying to use absolute paths, but that won't work (as far as I can tell ) in my instance as the directory returns a 404, it is not an issue of not directing to the proper directory.

    Here are the contents of my .htaccess:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]

    This is my first mod rewrite and I am still getting my head around what all this means. Hopefully someone has a solution as I would prefer to NOT advertise that I am using php to minimize hack attempts (how ever minutely it will minimize it is still something).

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  2. #2
    SitePoint Zealot GHicks's Avatar
    Join Date
    Oct 2003
    Location
    Prescott Valley, Arizona, United States
    Posts
    135
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As usual, I get frustrated, post a request for assistance, and then the solution slaps me in the face.

    I added CSS to the following line:
    RewriteCond $1 !^(index\.php|images|robots\.txt)

    To make it:
    RewriteCond $1 !^(index\.php|css|images|robots\.txt)

    It appears that tells it to ignore those directories? I was a little stumped because my images were showing, that is what made this solution pop out at me. Guess I need to read up on rewrite rules and how they are structured.

    Greg
    Greg Hicks
    Tekamba Computers, LLC
    greg@tekamba.com
    http://tekamba.com

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,156
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    For what it's worth, this problem is more commonly solved like this:

    RewriteCond %{REQUEST_FILENAME} !-f

    This says, apply the rewrite only if the request is *not* for a real file. Real files include the index.php file, the robots.txt file, all your image files, all your CSS files, and any other real file you might not have thought of (such as JS files or favicon.ico).
    "First make it work. Then make it better."

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Greg,

    That's the problem with (.*):

    [rant #1]
    The use of "lazy regex," specifically the EVERYTHING atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is "greedy." Unless you provide an "exit" from your redirection, you will ALWAYS end up in a loop!
    [/rant #1]

    Okay, !-f should take care of your problems although I prefer to use my mod_rewrite to screen requests (you had EVERYTHING/NOTHING redirected to index.php). Don't you just want all .php scripts other than index being redirected (leaving images, css and js alone)?

    Put yourself in the position of mod_rewrite and think about your redirectionS - mod_rewrite is restarted every time a match/redirection is successful so "get loopy" with mod_rewrite and you'll see the effect of bad code.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,156
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    It's worth noting that the use of (.*) is both standard practice and frequently used even in the official documentation itself. There's nothing wrong or lazy about it. This case in particular seems to be a perfectly good use for it. If the goal is to match any request that isn't a real file, then (.*) will match any request, exactly like we want, and the condition will exclude real files, exactly like we want.
    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •