由于网址重写,CSS,图片和JS无法加载 [英] CSS, Images, and JS not loading due to url rewrite
问题描述
当我的网址以/结尾时,我无法加载CSS和图片。我正在关闭本地主机,并且以以下格式重写网址:
I'm having trouble with css and images loading the moment my url ends in a /. I'm working off my localhost and the urls are rewritten in the form:
localhost/mysite/public/user/testuser/books/1234
随着请求加载testuser的用户个人资料页面并更改页面状态以关注一本ID为1234的书。
With the request loading the user profile page of testuser and changing the page state to focus on a book with the id 1234.
一切都可以在localhost / mysite / public / user上正常运行,但是当我向CSS url中引入/或/ testuser时, JS和图像中断。我确实有url重写的地方,并阅读了许多说明其.htaccess重写问题的帖子。这是我网站的结构:
Everything works fine with localhost/mysite/public/user, but the moment I introduce a / or /testuser to the url the CSS, JS, and Images break. I do have url rewriting in place and read a number of posts that state its a .htaccess rewrite problem. Here is the structure of my site:
mysite/
app/
controllers/
models/
etc...
public/
css/
styles.css
images/
profilepic.png
js/
index.php
所有图像,js和CSS均通过相对路径引用:ex:。 ./public/css/styles.css。
All images, js, and css are referenced via relative path: ex: ../public/css/styles.css.
.htaccess的内容是:
Contents of .htaccess are:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
我读了各种解决方案的组合,例如,将所有路径更改为绝对路径或将其更改为/css/styles.css。绝对当然可以,但是当我将网站移至其实际域时,这没有任何意义。我需要更改每个实例。
I read a mix of solutions including changing all the paths to absolute or changing them to /css/styles.css for example. Absolute of course works, but that doesn't make sense when I move the site to its actual domain I would need to change every instance.
关于使用以下方法从重写中排除css,jpeg等文件:
There was also a note about excluding css, jpeg, etc... files from the rewrite using:
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
这些选项均无效,而且我不确定应该在哪里查看。有任何想法吗?
Unfortunately none of these options worked and I'm not sure where else I should look. Any ideas?
推荐答案
如果您不想在移动网站时更改每个实例,只需更新<$ c html页面标题的$ c>< head> 部分,并将其添加
If you don't want to change every instance when you move your site, just update the <head>
section of your html page header and add this
<base href="http://www.example.com/" />
或
<base href="/" />
这将解决该问题,您不必将所有实例都更新为绝对路径。
This will take care of that problem and you don't have to update all instances to absolute paths.
这篇关于由于网址重写,CSS,图片和JS无法加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!