Ali Mhanna
Ali Mhanna

Reputation: 209

how to fix css loading problem on production server

my website is now hosted on a server and the URL is like this

test1.test.com 

but now I have to move it to another hosting which they don't have this ability and I had to change it to:

test.com/test1

until now everything is working except the CSS and JS files I get 404 not found error.

and in my case, I didn't change anything and this is how I am loading the CSS files

        <link rel="stylesheet" href="{{ asset('css/main.css') }}">

does anyone have any idea how can I fix this? of course, i tried to add the (test1) before the {{ asset('css/main.css') }} but it didn't change anything.

PS: the source code is not saved under sub directory the /test1 is just routing to a load balancer.

and here are the apache2 setting file for docker image:

    <VirtualHost *:80>
    DocumentRoot /var/www/html/web
    <Directory /var/www/html/web>
        AllowOverride None
        Require all granted

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>

    ErrorLog /var/log/apache2/app_error.log
    CustomLog /var/log/apache2/app_access.log combined
</VirtualHost>

the deployment is on a cloud server using docker image and locally it's working in any way I am accessing the website. any ideas or setting it might help? thanks in advance

Upvotes: 5

Views: 4816

Answers (2)

Ali Mhanna
Ali Mhanna

Reputation: 209

i just want to thanks every one tried to help me with my problem but now i found the problem and the solution:

the problem has nothing to do with symfony it self but it was related to the hosting service its seems to be that the service provider is using ingress and Kubernetes and ingress does not support static files (css, js) loading. there is work arounds if you have access to the server settings files but in my case there was not.

my solution was to serve the static files as a service. its somthing like private CDN for my assets and all the static files.

thanks again and if any one had the same case i will be more than happy to help.

Upvotes: 0

exside
exside

Reputation: 3894

It uses a relative path, so if your "base"-path is /test1, a relative path would look in a folder called /test1/css/ for the css file.

Depending on your symfony version, the asset() function allows an absolute config parameter to make the path absolute, therefore: what happens if you include the static files like this:

<link rel="stylesheet" href="{{ asset('css/main.css', absolute=true) }}" />

if you're on a version later than 3.0, use:

<link rel="stylesheet" href="{{ absolute_url(asset('css/main.css')) }}" />

Upvotes: 5

Related Questions