hardfork
hardfork

Reputation: 3261

Rewrite rule so that css and js files with absolute paths are loaded correctly

It took me 2 hours but I can't figure it out and I don't know how to google a solution.

This is my Rewrite-Rule in the .htaccess-file:

RewriteCond %{REQUEST_URI} ^/blog/(.*)
RewriteRule ^blog/(.*) http://localhost:2368/$1 [P]

When I call example.com/blog, it redirects my to my ghost-blog running in nodejs (port 2368).

The problem: The blog itself loads css and js files with absolute paths:

<link rel="stylesheet" type="text/css" href="/assets/built/screen.css" />.

So it tries to load example.com/assets/built/screen.css.

But the file is reachable at example.com/blog/assets/built/screen.css

How can I solve this?

Upvotes: 2

Views: 585

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40434

You have 3 alternatives:

  • Change the links/scripts source to /blog/assets/...

  • Change the file locations to /assets

  • Use a RewriteRule

.htaccess

RewriteCond %{REQUEST_URI} ^/assets(.*)\.(css|js)$
RewriteRule ^(.*) /blog/$1 [L]

RewriteCond %{REQUEST_URI} ^/blog/(.*)
RewriteRule ^blog/(.*) http://localhost:2368/$1 [P]

Here's the rule working: .htaccess tester

I assume that the files are being served by apache2 and not by the ghost-blog running in node.js. But if it's the latter,it will still work since after redirecting it will go the next rule. But you can also do the following:

RewriteCond %{REQUEST_URI} ^/assets(.*)\.(css|js)$
RewriteRule ^(.*) http://localhost:2368/$1 [L]

Upvotes: 4

Related Questions