Welcome Guest, Not a member yet? Register   Sign In
Mod_rewrite, /controller/function, and CSS/JS
#1

[eluser]underskor[/eluser]
Would someone care to explain how to use mod_rewrite, with /controller/function/argument URI's while allowing CSS/JS/images to continue functioning?

Using the .htaccess sample provided on the forums/wiki seems to get me as far as /controller - awesome - but once I go into /controller/function, I lose my stylesheets etc. I imagine they are being rewritten to /controller/function/css/style.css, but I can't figure out how to stop that.

My current .htaccess:
Code:
RewriteEngine On

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|css)
RewriteRule ^(.*)$ index.php/$1 [L]

I think my problem is that I do not understand the last RewriteCond fully. I think it's something like 'apply the following RewriteRule if the pattern in RewriteRule is not 'index.php' or 'css'. I've tried changing 'index.php|css' around but had no luck. Would be great if someone could explain it for me.

Directory structure (FWIW):
Code:
/public_html
  index.php
  /css
    style.css
    /imgs
      img.gif
    /dir
      style.css

Thanks!
underskor
#2

[eluser]jtkendall[/eluser]
Hi underskor,

Can you post your html lines for the CSS files? My guess would be that you need to use base_url() in front of the file location, for example:

Code:
<link rel="stylesheet" type="text/css" href="<?=base_url();?>style.css" />

or you can use a / and have it look at the web root for it. For example, I always put my css files in /assets/css/ so when I call one I use:

Code:
<link rel="stylesheet" type="text/css" href="/assets/css/style.css" />

Haven't had any problems with it (so far).
#3

[eluser]underskor[/eluser]
Yeah I am using ./css/style.css to reference the CSS files as I have multiple local projects I am working on.
Code:
/project1
  /public_html/
/project2
  /public_html/
etc

It would make sense to use /css/style.css - it would work on a production server - but on my development machine I would have to change the paths.

Base_url() it is then! I will give it a go and post back.

EDIT: Works great. Thanks Jtkendall.




Theme © iAndrew 2016 - Forum software by © MyBB