williamg
williamg

Reputation: 2758

mod_rewrite causes loss of stylesheet

I'm using mod_rewrite to clean up some of my URL's (duh) and I got it working. Sorta. It redirects to the correct page, but the stylesheet doesn't show up. Here's my code in .htaccess:

RewriteEngine on
RewriteRule ^blog/([^/\.]+)/?$ post.php?page=$1 [L]

So what's going on? This is my first time working with mod_rewrite, by the way.

Upvotes: 0

Views: 1588

Answers (3)

Amadiere
Amadiere

Reputation: 11426

It could be your CSS link is broken by your new URLs if you are using relative links. e.g:

Stylesheet link used: ../css/stylesheet.css
Old page: /blog/example/post.php
New page: /blog/2009/12/example/post.php

This break would affect images too. An easy way to fix this is to decide upon the definitive URL for the CSS and absolute link it. e.g.

/css/stylesheet.css

This would mean that irregardless of where you are in the structure, the CSS (or images, if you follow this routine) would show up ok.

Upvotes: 4

Gumbo
Gumbo

Reputation: 655755

It’s probably just your new URLs that cause that relative URLs are resolved differently than with your old URLs. Try it with an absolute URL path like this:

/style/screen.css

Instead of a relative URL path like:

style/screen.css
./style/screen.css

Upvotes: 1

Tyler Carter
Tyler Carter

Reputation: 61587

Your CSS (Lets Assume /blog/style.css) is going to post.php?page=style.css

To Fix, I'd suggest putting this right after the RewriteEngine

RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{SCRIPT_FILENAME} -d

That will forward all requests where the file exists, like a CSS/JS file, directly to the file.

In the future, I suggest browsing to the URL on a browser, and see where it leads you to. If it doesn't work in the browser, it won't work for the page.

Upvotes: 2

Related Questions