Christian Burgos
Christian Burgos

Reputation: 1591

cakephp 2 css, javascript and links not working on local machine/localhost

what htaccess settings should i use on the webroot folder. it seems that it's the primary culprit of why all the links in the Layout doesn't work.

view

enter image description here

here is the htaccess code inside the webroot folder

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

the project is from the live server but i needed to make it work on my local machine/ localhost so that i can make some changes. but as you can see on the first picture, the css, links and javascript codes doesn't load.

Upvotes: 2

Views: 1220

Answers (1)

Szymon
Szymon

Reputation: 1415

The core of your problem is that on localhost you are running your website in subfolder in Apache document root, and you do not have this subfolder specified in your links. So if file resides in C:/xampp/htdocs/www/app/webroot/css/mysheet.css, correct way to reach it would be <link rel="stylesheet" href="/www/css/mysheet.css">.

You have two options here, and you can (and I think you should) use BOTH of them:

  1. Use HtmlHelper

By calling $this->Html->css("mysheet") in your view, Cake will create link to your sheet, and it will include also its base folder. More info: HtmlHelper

  1. Use local domain

You can set up local virtual host in your xampp config. After doing so, you will be able to reach your local website using this domain, instead of localhost/www. In example below I will use domain mywebsite.local:

2.1. Go to C:/xampp/apache/conf/extra and open httpd-vhosts.conf.

2.2. Add following code to end of file:

<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/www" ServerName mywebsite.local </VirtualHost>

2.3. Go to C:/Windows/System32/drivers/etc and open hosts file with admin permissions.

2.4. Add following line: 127.0.0.1 mywebsite.local

2.5. Restart Apache.

Now, you can access your local website by typing in your browser address bar http://mywebsite.local. As document root for this virtual host is now htdocs/www, all css should load.

Upvotes: 1

Related Questions