aborted
aborted

Reputation: 4541

Apache mod_rewrite not letting images (and other assets) be loaded

So this is currently my .htaccess file:

AddHandler application/x-httpd-php .php .ru
DirectoryIndex tsuki.ru index.html

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /tsuki/

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ - [L]

# Rule to display data via ID
RewriteRule ^([^/]+)/([0-9]+)/?$ tsuki.ru?uri=$1&id=$2 [QSA,L]

# Display actions from controller
RewriteRule ^([^/]+)/([^/]+)/?$ tsuki.ru?uri=$1&action=$2 [QSA,L]

# Display specific ID for edit
RewriteRule ^([^/]+)/edit/([0-9]+)$ tsuki.ru?uri=$1&action=edit&id=$2 [QSA,L]

# Destroy specific ID
RewriteRule ^([^/]+)/destroy/([0-9]+)$ tsuki.ru?uri=$1&action=destroy&id=$2 [QSA,L]

# Display controller only
RewriteRule ^(.*)$ tsuki.ru?uri=$1 [QSA,L]

And when I remove these rewrite rules, images and other assets load fine (JS, CSS etc.). How can I tweak my rewrites so assets can be loaded with relative paths?

Upvotes: 1

Views: 63

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19016

Your two RewriteCond are only applied to very next rule.
This means all your assets are probably rewritten with this rule

RewriteRule ^(.*)$ tsuki.ru?uri=$1 [QSA,L]

You can refactor your code this way

AddHandler application/x-httpd-php .php .ru
DirectoryIndex tsuki.ru index.html

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /tsuki/

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Rule to display data via ID
RewriteRule ^([^/]+)/([0-9]+)/?$ tsuki.ru?uri=$1&id=$2 [QSA,L]

# Display actions from controller
RewriteRule ^([^/]+)/([^/]+)/?$ tsuki.ru?uri=$1&action=$2 [QSA,L]

# Display specific ID for edit
RewriteRule ^([^/]+)/edit/([0-9]+)$ tsuki.ru?uri=$1&action=edit&id=$2 [QSA,L]

# Destroy specific ID
RewriteRule ^([^/]+)/destroy/([0-9]+)$ tsuki.ru?uri=$1&action=destroy&id=$2 [QSA,L]

# Display controller only
RewriteRule ^(.*)$ tsuki.ru?uri=$1 [QSA,L]

Note: don't forget to use absolute paths instead of relative paths for all your ressources (images, js, css, href links, etc)

Upvotes: 1

Related Questions