Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite, css and rewritecond

Please help a newbie understand mod-rewrite!

         

nickyc

3:03 pm on Aug 8, 2011 (gmt 0)

10+ Year Member



Having problems with modrewrite interfering with my css paths.

I want turn
http://localhost/snips/bandb/profile
into
http://localhost/snips/bandb/home.php pagename='profile'

My css paths look like:
http://localhost/snips/bandb/css/bb.cs


My htaccess currently has:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/snips/bandb/([0-9a-zA-Z_\&\-\ \.\/]+)$ /snips/bandb/home.php?pagename=$1 [L]


it is ignoring the rewritecond lines and rewriting the css path

If i replace them with:
RewriteCond %{REQUEST_URI} ^/(cgi-bin|css|images)/?.*$ [NC]


it no longer replaces the css, but it doesn't rewrite anything else either.

lucy24

3:16 pm on Aug 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is going to sound patronizing, but it works. Honest. Start by explaining in English exactly what it is you want to do, including who er, that is, what files you want to do it to and under what circumstances.

nickyc

3:25 pm on Aug 8, 2011 (gmt 0)

10+ Year Member



I know it should have worked, but I promise, it didn't. I've been trying it all day!

Have just found a way that does work.

Using the uncommented lines everything is fine.

If I use the REQUEST_FILENAME lines, my page has no css applied, and if I go to [localhost...] directly, it converts it into my home page (ie has been rewritten by htaccess and processed as an unknown page by my cms)

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} ^/(cgi-bin|css|images)/?.*$ [NC]
RewriteCond %{REQUEST_URI} !^/snips/bandb/css/
RewriteCond %{REQUEST_URI} !^/snips/bandb/images/
RewriteCond %{REQUEST_URI} !^/snips/bandb/js/
RewriteRule ^/snips/bandb/([0-9a-zA-Z_\&\-\ \.\/]+)$ /snips/bandb/home.php?pagename=$1 [L]

nickyc

3:30 pm on Aug 8, 2011 (gmt 0)

10+ Year Member



Would still like to know why.

Not sure how else to explain what I was tryng to do - If i type in
http://localhost/snips/bandb/profile

I want modrewrite to pass it through as
http://localhost/snips/bandb/home.php?pagename=profile


It was doing that ok , but at the same time, my css file
http://localhost/snips/bandb/css/bb.css
was also being rewritten.

lucy24

11:19 pm on Aug 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The combination of relative linking (I assume that's how you call your shared css) with rewrites is potentially lethal. That is, it won't crash your site, but nothing will be able to find the CSS once you're done.

Have you got a RewriteBase lurking in the background somewhere?

g1smd

11:35 pm on Aug 8, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What does the link to the CSS file, as found on your HTML page, look like?

Does it use a relative, root relative, or absolute URL in that link?

nickyc

7:40 am on Aug 9, 2011 (gmt 0)

10+ Year Member



My css paths are absolute -
http://localhost/snips/bandb/css/bb.cs


Just realised on my localhost I am using httpd.conf for the rewrites. On my live site i will be using .htaccess. Does that make a difference?

I can't find anything about a RewriteBase in my httpd.conf.

lucy24

7:54 pm on Aug 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whoops. You meant literally localhost. I thought you were just saying that to obfuscate your domain name.

In general you don't need a RewriteBase in htaccess. It will default to the directory your htaccess is in. I only asked about it because it's one explanation for the CSS going walkabout.

Do you have a single shared css used by different files in different directories at different levels? If so, root-relative is safest. Relative links are easy to work with but you have to be very careful, especially when you throw rewrites into the mix. (For things like custom 403 or 404 pages, which are really just a special kind of rewrite, I gave up and put the needed CSS in the file itself :)) Absolute links for CSS is definitely overkill.