URL 重写后缺少 CSS 文件和图像

时间:2023-02-27
本文介绍了URL 重写后缺少 CSS 文件和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模式重写制作用户友好的 URL.我的问题是,在为我的 URL 提供名称"之类的类别后,当我使用新 URL 调用页面时,它无法加载 CSS 文件或图像.

I'm trying to make user friendly URL using mode rewrite. My problem is, that after giving category like 'name' to my URL, when I call the page using new URL, it can't load the CSS file or images.

我有一个链接:

localhost/mywebsite/project?id=22

localhost/mywebsite/project?id=22

新链接类似于

localhost/mywebsite/project/22/myproject.project

localhost/mywebsite/project/22/myproject.project

htaccess 代码:

htaccess code:

RewriteRule ^project/([0-9]*)/.*.project$ /project.php?project=$1 [L]

(它可能不是 100% 正确,但我现在无法访问我的代码,所以我只是写了这个,它在原始源代码上运行良好)

(it might not be 100% right but I don't have access to my code right now so I just wrote this and it works fine on the original source)

我的根目录是 localhost/mywebsite/

My root directory is localhost/mywebsite/

我的 CSS 文件在 css/style.css

and my CSS file is in css/style.css

localhost/mywebsite/css/style.css

localhost/mywebsite/css/style.css

我的 htaccess

my htaccess

localhost/mywebsite/.htaccess

localhost/mywebsite/.htaccess

我的project.php文件在

and my project.php file is in

localhost/mywebsite/project.php

localhost/mywebsite/project.php

所以在项目页面中,我可以使用相对路径访问 CSS 文件,

So in the project page I have access to CSS file by using relative path,

<link href="css/style.css" rel="stylesheet" type="text/css" />

但是当我使用重写的 URL 页面时找不到 CSS 文件.

but when I use rewritten URL page can't find the CSS file.

我还没有域名,所以不能使用绝对路径和域名!它可以是任何东西.

I can't use absolute path with domain name because I don't have domain yet! and it can be anything.

一种方法是按照类似问题的建议使用域的相对路径本地主机/我的网站/project.php当我在本地运行脚本时,我的根目录是本地主机所以css链接应该看起来像

one way is to use relative path to domain as suggested on the similar questions localhost/mywebsite/project.php and when i run my script localy my root directory is localhost so css link should look like

href="mywebsite/css/style.css"

href="mywebsite/css/style.css"

但是当我上线时,我应该将所有链接更改为可能类似于

but when i go live i should change all links to probably something like

href="/css/style.css"

href="/css/style.css"

这似乎有很多工作

推荐答案

为您的本地版本添加

<base href="//localhost/mywebsite" />

到头部部分

对于您的实时版本,请将其更改为

and for your live versions change it to

<base href="//your.domain.here" />

参考 http://www.w3.org/TR/html4/struct/links.html#h-12.4

这篇关于URL 重写后缺少 CSS 文件和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:PHP .htaccess ->漂亮的网址(反向) 下一篇:如何使用 mod_rewrite 显示 SEO 友好的 URL?

相关文章

最新文章