問題描述
我正在嘗試使用模式重寫制作用戶友好的 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="http://localhost/mywebsite" />
到頭部部分
對于您的實時版本,請將其更改為
and for your live versions change it to
<base />
參考 http://www.w3.org/TR/html4/struct/links.html#h-12.4
這篇關于URL 重寫后缺少 CSS 文件和圖像的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!