solving the issue of CSS not loading properly on the live server

My current website is hosted on a server with the URL as follows:

test1.test.com 

However, I need to move it to another hosting service that does not support this configuration. I had to change the URL to:

test.com/test1

Everything seems to be working fine after the migration except for the CSS and JS files - I keep getting a 404 not found error.

I haven't made any changes to how I load the CSS files, it's still done like this:

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

Does anyone have any suggestions on how to resolve this issue? I have tried adding "test1" before {{ asset('css/main.css') }} but it has not made a difference.

For context, my source code is not stored in a subdirectory, the /test1 simply routes to a load balancer.

Below are the Apache2 settings for the 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 a Docker image and locally it works fine regardless of how I access the website. Any suggestions or adjustments that might help fix the issue?

Answer №1

When you specify a relative path in your code, it will search for the file within a specific directory based on your base path. For example, if your base path is set to /test1, a relative path would be looking for the css file in a folder called /test1/css/.

The asset() function in Symfony has an option to make the path absolute by using the config parameter "absolute". So, what happens when you include static files like this:

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

If you are using a Symfony version above 3.0, you can use the following syntax:

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

Answer №2

Gratitude to everyone who tried to assist me with my issue. I have pinpointed the problem and its solution:

The issue was not directly related to Symfony itself, but rather to the hosting service being used. It appears that the service provider is utilizing ingress and Kubernetes, where ingress does not support the loading of static files (such as CSS and JS). While there are workarounds available if you have access to server settings files, in my case, this option was not viable.

I decided to serve the static files as a service, essentially creating a private CDN for my assets and all static content.

Once again, thank you. If anyone else encounters a similar situation, feel free to reach out - I am more than willing to offer assistance.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Resolving the Persistence Problem of Navigation Bar Fading In and Out

I've been struggling for hours trying different methods to achieve the desired outcome, but unfortunately, none of them have worked as expected. My goal is simple: I want the fixedbar to fade in when the scroll position exceeds the position of the ph ...

The alignment in Firefox and Google Chrome does not appear to be consistent

The appearance of the content is correct in Google Chrome but incorrect in Firefox. Does anyone have any suggestions on how to fix this for Firefox? ...

Placing Bootstrap nav-items outside of the justify-content-center alignment

Having trouble positioning a button in the nav container to the right side of the screen while keeping all other nav items centered using justify content center. I've tried the following solutions: Using margin-left:auto on the button, but it shifts ...

JS animation triumphant

I have developed an app that utilizes a checkbox to control the appearance of an overlay on a screen. To ensure smooth transitions, I have incorporated animations into the process. #overlay{ position:absolute; height: 100vh; width: 100vw; ...

What are the best practices for effectively utilizing Media Queries?

Currently, I am in the process of developing 3 HTML5 responsive websites that will need to be optimized for various devices. So far, I have successfully implemented media queries for smaller devices such as 320px, 375px, 425px, 480px, 768px, and 1024px. H ...

Determining the identity of an HTML element using its surrounding values

I have a webpage with a table displaying various services and their corresponding download buttons. Here is an example of the content: nov. service 1 [ click to download pdf ] nov. service 5 [ click to download pdf ] nov. service 3 [ ...

Incorporating a classList.toggle into a snippet of code

button, p, h1, h2, h3, h4, h5, a{ /* Define specific elements to use "fantasy" font */ font-family: Tahoma; } #main_body{ margin: 0px auto; background-color: #dedede; } #top_body{ /* Remove margin for simplicity */ } #t ...

Why doesn't the Kellum method for CSS image replacement work with button elements?

Recently, I discovered that the kellum method doesn't work as effectively with HTML button elements. Specifically, it does work but requires additional text indent. To sum it up, here is the technique: text-indent: 100%; white-space: nowrap; overflow ...

I am looking for a way to retrieve the ids of all div elements that have the same x coordinate using document.elementFromPoint in JavaScript. Can someone help me with

Currently, I am facing an issue where I have two divs positioned at the same x coordinate. I am attempting to retrieve the IDs of both divs using document.elementFromPoint(). However, I am only able to receive the ID of one div. var elem = document.elem ...

The progress bar in vis.js is displaying inaccurate values

I am currently facing an issue with implementing the progress bar. The problem is that the progress bar always shows around 55% regardless of the value being set. var items = new vis.DataSet([ {id: 0, group: 0, content: 'item 0',value: 0.0, st ...

Creating a uniform layout with Flexbox: Equal-height columns with aligned tops

In the application, I have implemented a flexbox layout similar to the code snippet below, containing a group of li elements with three divs in each. .container { width: 500px; } ul { list-style: none; margin: 0; padding: 0; display: flex; } l ...

Issue with hamburger icon in Bootstrap navbar following height adjustment of the navbar

Although my navbar was functioning properly with the hamburger menu expanding down as expected, I encountered an issue after adjusting the height of the navbar. The hamburger button still worked and expanded the menu, but it only displayed the first item o ...

Spin rectangular image within boundary using angular 6

I am trying to achieve a rotating effect on a rectangular image within a frame of the same dimensions, as shown below: <div style="border:1px solid #d3d3d3;width:208px;height:250px"> <img style="width:208px;height:250px" src="https://www.webslak ...

What could be causing this slider to malfunction in Firefox?

I have recently developed a slider on this page: While the slider functions smoothly in Chrome, I am facing compatibility issues with Firefox. Can anyone shed some light on why this might be happening? Here is the HTML, CSS, and JS code used for the slid ...

Hey there, I have a header.css file!

My React project is experiencing issues with the CSS file. The first header in the CSS file works fine, but the second header and every other class created after it are not responsive. Only the 'header' class is properly linked to Header.css file ...

How can the $spacer variable in Bootstrap be modified?

My attempt to adjust the $spacer variable in SASS to increase margins seems to be failing. Here is my code: @import url(../node_modules/bootstrap/scss/_functions.scss); @import url(../node_modules/bootstrap/scss/_variables.scss); @import url(../node_module ...

Troubleshooting: EJ Syncfusion React Scheduler Issues

I have recently implemented a syncfusion calendar for managing appointments in my ReactJs project, following the code examples from this link . After editing the JS and CSS codes (available at ), I encountered an issue with the CSS not displaying correctl ...

Angular: "btn" class vanishes when the button is toggled

I am facing an issue with the button's class change functionality. I am using the [ngClass] directive to switch between Bootstrap classes for the button style. However, when I click the button, the "btn" class seems to disappear from the code. Instead ...

What is the best way to postpone the rendering of a table?

My table is bogged down by an excessive number of rows, causing a sluggish rendering process. I'm curious about the best method to address this issue. Are there any existing libraries specifically designed for this purpose? ...

Aligning a DIV using javascript

Hey everyone, I'm encountering an issue with the JavaScript on my website. I'm struggling to center the div in order to properly display the image I click on. It seems to work fine on the second attempt, but on initial click, the div always appea ...