Trouble with including Javascript files


I’m trying to include my own javascript as a separate file but am running into problems, I wonder if someone can help.

So I have the file saved under the view for my particular model in its own directory, e.g:


And then in protected/views/user/show.php I am including this code:

$path = CHtml::asset(dirname(__FILE__)."/assets/test.js");

Yii::app()->clientScript->registerScriptFile($path, CClientScript::POS_HEAD);

The corresponding asset is being created (i.e. assets/djgsdj12rw/test.js) and the script tags are included inside the head tags. However the file is not being read, instead I get an error like:


2 "-//W3C//DTD XHTML 1.0 Transitional//EN"

3 "">

4<html xmlns="" xml:lang="en" lang="en">


6<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

7<title>Page Not Found</title>

8<style type="text/css">


10body {font-family:"Verdana";font-weight:normal;color:black;background-color:white;}

11h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }

12h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }

13h3 {font-family:"Verdana";font-weight:bold;font-size:11pt}

14p {font-family:"Verdana";font-weight:normal;color:black;font-size:9pt;margin-top: -5px}

15.version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;}





20<h1>Page Not Found</h1>

21<h2>Unable to resolve the request &quot;assets/djgsdj12rw/test.js&quot;.</h2>


23The requested URL was not found on this server.

24If you entered the URL manually please check your spelling and try again.



27If you think this is a server error, please contact the webmaster.


29<div class="version">

302009-09-09 14:29:51 Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7d DAV/2 PHP/5.2.6 mod_perl/2.0.2 Perl/v5.8.4 <a href="">Yii Framework</a>/1.0.6</div>



Any help would be much appreciated!

P.S I have attempted to search for this on the forums, but as far as I can tell the new forums search functionality sucks!

Check out this thread:


The server seems to redirect the request to your entry script even though the files exist. Do you have these lines in your .htaccess?

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d


They must be right before your RewriteRule…

Got it, thanks to you Mike. My config is actually in the servers httpd.conf rather than .htaccess so for the benefit of others, this is what I now have and it appears to work:

<Directory "/path/to/app">

        Options +FollowSymLinks

        IndexIgnore */*

        RewriteEngine on

        RewriteCond %{REQUEST_FILENAME} !=f

        RewriteCond %{REQUEST_FILENAME} !=d

        RewriteRule . index.php


<Directory "/path/to/app/css">

        RewriteEngine off


<Directory "/path/to/app/assets">

        RewriteEngine off


<Directory "/path/to/app/js">

        RewriteEngine off


Just the RewriteCond statements on their own do not appear to have the desired affect as I already had those in place, I also had the CSS directory statement in place which explains one of the things that was really confusing, in that I could directly access js files if I stored them in that directory, but couldn’t if they were anywhere else.



Strange. You say that it doesn’t work without the <Directory…> directives? That should not be the case. You can enable logging for mod_rewrite to find out what’s going on.

Don’t worry, it’s me be an idiot. I was using != in the RewriteCond statements, instead of !- Useful link though, I’ve always kind of blindly followed half-baked tutorials for my Apache config needs in the past, I’ll have a read through that and hopefully glean some kind of knowledge :slight_smile:

Good luck! Always remember: ;)