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

不羁的心 提交于 2020-01-24 14:08:17

问题


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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/44601671/cakephp-2-css-javascript-and-links-not-working-on-local-machine-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!