Thursday, March 31, 2011

mod_rewrite and CSS

I'm using mod_rewrite to change all the URLs on a site. Example: http://www.site.com/about/ becomes http://www.site.com/?action=about (don't worry, I have nice friendly sanity checking). The problem is that for anything other than http://www.site.com/ the CSS stylesheet won't load. I assume this is because it looks for the stylesheet in /about/content/style.css instead of /content/style.css. How do I make sure it finds the right stylesheet?

Here's some of my code just in case. .htaccess looks like this:

RewriteEngine on
RewriteRule ^news/([0-9]+)/?$ /?action=news&start=$1 [L]
RewriteRule ^news/?$ /?action=news&start=0 [L]
RewriteRule ^(about|contact|man|home|download)/?$ /?action=$1 [L]

They all redirect to index.php, which starts off like this:

<html>
  <head>
    ...
    <link rel="stylesheet" href="content/style.css" type="text/css" />
  </head>
  <body>
    ...
  </body>
</html>
From stackoverflow
  • Try this.

    <link rel="stylesheet" href="/content/style.css" type="text/css" />
    
    Chris Lutz : Wow. I feel like an idiot now.
  • Why not just use an absolute URI for your CSS file? ie /content/style.css. Saves headaches like this.

  • Another solution would be setting the base URI using the BASE HTML element:

    <base href="/" />
    

    Thereby relative URIs are resolved from the base URI path / and not the URI path of the current ressource (in your case /about/).

0 comments:

Post a Comment