0

I have been looking around for a while but even after all the stackoverflow and other content I haven't got this to work.

I can turn AngularJS 1.x urls into pretty urls (so removing the fragment by replacing # by /) just fine when starting from the homepage. But on any other page, the javascript, css and other files can't load because the url to them is wrong. For example:

Correct url: localhost/css/app.css

Wrong url: localhost/about/css/app.css

On an nginx I fixed this as follows:

  location ~ .(css|files|js|lib|partials)/(.+)$ {
            try_files $uri $uri/ /$1/$2;
        }

        location / {
            index  index.html index.htm;
            try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
        }

But I need to do it for Apache as well. The closest I get is :

RewriteCond %{REQUEST_URI} \.(lib|css|js|partials|files)$ [NC]
RewriteRule ^(.*)$ ./index.html/$1 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} –d
RewriteRule ^ - [L]

…And all kinds of variations to these rules but I can only get the files to load correctly from the homepage. I either don't get it to point to the right url or it does point to it but it doesn't actually load the files.

Surely lots of people implemented this before for an AngularJS application?

Thanks in advance, Arno

Sleenee
  • 594
  • 1
  • 8
  • 21
  • It doesn't look like apache problem to me, it isn't supposed to serve static files from relative locations. You need to specify tag. – Estus Flask Feb 20 '17 at 11:49
  • angularjs is correctly configured as on an nginx it all works as intended. And the is also present on the index.html . – Sleenee Feb 20 '17 at 12:58
  • The fact that `localhost/about/css/app.css` and not `localhost/css/app.css` url is requested suggests that the problem is on client side. – Estus Flask Feb 20 '17 at 13:07
  • in index.html I have this: , in app.js I have : $locationProvider.html5Mode({ enabled: true, requireBase: true, rewriteLinks : true }); So what else am I missing? Don't forget, on an nginx I got it to work with this so that pushes me to think it's the .htaccess – Sleenee Feb 20 '17 at 13:26
  • Then obviously, doesn't work as you expect, this is the real issue. I would suggest to re-ask the question according to that and provide some html code to replicate it. Can't say anything on that as I'm always sticking to absolute paths for resources. You probably fixed it in nginx but this is a hack that solves XY problem. – Estus Flask Feb 20 '17 at 13:59

0 Answers0