开发者

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 firs开发者_开发问答t time working with mod_rewrite, by the way.


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜