Ryan
Ryan

Reputation: 667

mod_rewrite not working correctly - accessing static resources

Right now I have a few folders (img, css, js, and ico) in the root directory of my website. However, I want to move them into a new directory called public_html. For example, instead of the image folder being located in /img, it would be instead located in /public_html/img. But I'm having problems doing this, and i suspect it's a problem with my .htaccess file.

Options -Indexes
Options +FollowSymLinks
RewriteEngine On

ErrorDocument 404 static/404.html
ErrorDocument 403 static/403.html


RewriteCond %{REQUEST_FILENAME} !-f

# ---------- Custom Routes ----------------



# -----------------------------------------

RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2
RewriteRule ^(.*)$  index.php?r=$1  [L,QSA]

I can access /public_html/css/style.css just fine, but when I add in that first RewriteRule line and try to access /css/style.css, it doesn't work.

Can anyone figure out why? Thanks.

Upvotes: 2

Views: 83

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270677

Add the [L] flag to the first rule. Otherwise it drops through to the second rule and attempts to pass it in r= to index.php. Accessing it via public_html works because of the RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 [L]

# Update: Try using the RewriteCond before this line 
# as well as where you have it earlier
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$  index.php?r=$1  [L,QSA]

Upvotes: 1

Related Questions