Css and Js Files Not Found on Production Server

I am getting a 404 error on my production site for js and css files. The files are getting written to the assets directory so I dont think its a permissions folder but the browser is giving me a 404 not found error for all js and css, whether they are in assets directory or on /js and /css directories.

On production server in webroot directory:

drwxr-xr-x 2 www-data www-data 4096 Feb 4 13:24 css

drwxr-xr-x 9 www-data www-data 4096 Feb 5 08:43 assets

-rw-r–r-- 1 www-data www-data 3041 Feb 4 13:24 site.css

Any help on getting these files to show on browser is greatly appreciated.

I found the issue but I do not know how to fix it. The problem is that I need this to be a secure site so its at https. How do I make yii know that the url needs to be https and not http? I have both apache configurations pointing to the same directory. The only difference between the configurations is that one is 443 so it goes to https. The http site works fine, but I can’t get the css and js files to show up on the https site.

It’s strange.

What html does your site output?

For example, the following is from my site:




<!DOCTYPE html>

<html lang="ja-JP">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta name="csrf-param" content="_csrf">

    <meta name="csrf-token" content="xxxxx....xxxxx==">

    <title>Some Title</title>

    <link href="/assets/99e936a8/css/bootstrap.css" rel="stylesheet">

    <link href="/css/site.css" rel="stylesheet">

</head>

<body>

...



As you see, style sheets are referred without ‘http://’ or ‘https://’, and the code works fine both for http and https for me.

[EDIT]

Is it really a problem of http/https?

Try testing your production site with http.

I think I figured it out but have not tried it yet. I might have to




SetAlias('web','https://mydomainname.com');



My output is same as yours and the site works fine on the same server but on different Apache config without 443

Please stop doing things that you don’t understand ! ::)

If the output is the same, then the problem is at your server configuration - Yii has done it’s job properly.

Any hints in the logs? (runtime dir in web).

The apache configuration is exactly the same for both except one is set for 443 and the other for 80

Do you have a virtual host settings with one host being http(80) and the other https(443) with the same host name?

Yes. Eventually the http(80) setting will just redirect to https but for now I have the site on 80 until I can get this issue resolved.




<VirtualHost *:80>

        ServerName admin.example.com

	ServerAdmin webmaster@example.com

	DocumentRoot /var/www/example/backend/web/

        SetEnv COMPANY admin

  

        <Directory "/var/www/example/backend/web">

            Options Indexes FollowSymLinks

            AllowOverride all

            Order allow,deny

            Allow from All

            RewriteEngine On

            RewriteCond %{REQUEST_FILENAME} !-f

            RewriteCond %{REQUEST_FILENAME} !-d

            RewriteRule . index.php

        </Directory>


	ErrorLog ${APACHE_LOG_DIR}/admin_error.log

	CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>






<IfModule mod_ssl.c>

	<VirtualHost *:443>

		ServerAdmin webmaster@example.com

                ServerName admin.exampe.com

		DocumentRoot /var/www/example/backend/web/

                 

                SetEnv COMPANY admin

  

                <Directory "/var/www/example/backend/web/">

                    Options Indexes FollowSymLinks

                    AllowOverride all

                    Order allow,deny

                    Allow from All

                    RewriteEngine On

                    RewriteCond %{"REQUEST_FILENAME} !-f

                    RewriteCond %{"REQUEST_FILENAME} !-d

                    RewriteRule . index.php

                </Directory>


		ErrorLog ${APACHE_LOG_DIR}/admin_p4p_error.log

		CustomLog ${APACHE_LOG_DIR}/access.log combined


		SSLEngine on


                SSLCertificateFile /etc/ssl/certs/16f5fe7fec28f005.crt

                SSLCertificateKeyFile /etc/ssl/private/server.key


                SSLCertificateChainFile /etc/ssl/certs/gd_bundle-g2-g1.crt


		<FilesMatch "\.(cgi|shtml|phtml|php)$">

				SSLOptions +StdEnvVars

		</FilesMatch>

		<Directory /usr/lib/cgi-bin>

				SSLOptions +StdEnvVars

		</Directory>


		BrowserMatch "MSIE [2-6]" \

				nokeepalive ssl-unclean-shutdown \

				downgrade-1.0 force-response-1.0

		BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

	</VirtualHost>

</IfModule>




I guess one of the following will solve the problem.

  1. Define "SSLEngine off" in http site.

  2. Use a different name for http site, for example, "exadmin.exmaple.com".

  3. Delete http site settings completely.

The http site did not even exist when I was having this problem. I just created it temporarily while we test until I can get the https site working properly. Once I get the site to work I am just going to add a redirect on the http config. I need them both to be same url so that when person goes to http://example.com they will get redirected to https://example.com

Well, I would double check the ssl configuration. I don’t think Yii has something to do with this problem.

You can make a simplified html file something like the following for testing purpose:

[html]

<!DOCTYPE html>

<html>

<head>

&lt;title&gt;TEST&lt;/title&gt;


&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;/css/site.css&quot;&gt;

</head>

<body>

<h1>TEST</h1>

<p>It works!!</p>

</body>

</html>

[/html]

And put it in your web root, and see if your browser complains about site.css.

It only takes a couple of lines in your htaccess to redirect to https from http, so the config for http in your vhost file should be removed.

@sofark: I have the same problem with your basic example. You are right. It has nothing to do with Yii. But I am failing to see what is wrong with the SSL configuration. Would you have an example SSL configuration that I could look at? I am pretty sure it is the mod rewrite section that I am failing in.

I finally got it. I had the redirect initially on my https config, but the css and js files were not being found. So I took the redirect out and created a configuration for http (port 80) and that worked out fine but only for the http site. I removed the http site and this time I put the redirect on an htaccess file instead, and voila! Everything works now! I still think that I should be able to redirect on the apache config file but at least now I have something that works

Apache configuration is a dark and arcane art - congrats :)

@jacmoe It is indeed a dark place for me! I managed to get the simple test site to show up with css, but not so much luck with the yii framework project. If anyone could take a look at my https configuration and help me get it right it would be greatly appreciated. I have followed the examples on yii framework site but I really don’t know enough about apache configuration. I now have the http redirecting to https site but still no css or js.

Are the files physically present?

Yes. The files are physically present on the server. Some are on the asset folder created by yii and others are on the /css folder and /js folders themselves. They all get a 404 on the browser however. I don’t think its an issue with the actual certificate because I actually get a lock on the page, but I must be missing something on my apache configuration. This is my first time pushing a yii framework project to production. I have worked with zend framework before and I used to add a line on my config file to ignore css and js files from the rewrite condition. I don’t see that in the yii framework examples though so I’m guessing this is not needed.

And the permissions are OK? Viewable by all.

Should be. They work fine on http