URL重写后缺少CSS文件和图像 [英] Missing CSS file and images after URL rewrite
问题描述
我试图使用模式重写使用户友好的URL。
我的问题是,在给我的URL的类名name之后,当我使用新的URL调用页面时,它不能加载CSS文件或图像。
我有一个链接:
localhost / mywebsite / project?id = 22
新连结就像
localhost / mywebsite / project / 22 / myproject.project
htaccess代码:
RewriteRule ^ project /([0-9] *)/.* \.project $ /project.php?project=$1 [L]
(它可能不是100%正确,但我现在没有访问我的代码,所以我只是写这个,它在原始来源工作正常)
我的根目录是localhost / mywebsite /
我的CSS文件在css / style.css
localhost / mywebsite / css / style.css
my htaccess
localhost / mywebsite / .htaccess
我的project.php文件位于
localhost / mywebsite / project.php
< blockquote>
所以在项目页面中,我可以通过使用相对路径访问CSS文件。
< link href =css / style.css =stylesheettype =text / css/>
但是当我使用重写的URL页面时找不到CSS文件。
我不能使用域名的绝对路径,因为我还没有域名!它可以是任何东西。
一种方法是使用相似的路径域建议类似的问题
localhost / mywebsite / project.php
当我运行我的脚本localy我的根目录是
localhost
所以css链接应该像
href =mywebsite / css / style.css
但是当我上线时,我应该更改所有链接,例如
href =/ css / style.css
解决方案对于您的本地版本添加
< base href =// localhost / mywebsite/>
到头部
您的直播版本会将其更改为
< base href =// your.domain.here/&
参考 http://www.w3.org/TR/html4/struct/links.html#h-12.4
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.
I have a link like:
localhost/mywebsite/project?id=22
New link is something like
localhost/mywebsite/project/22/myproject.project
htaccess code:
RewriteRule ^project/([0-9]*)/.*\.project$ /project.php?project=$1 [L]
(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)
My root directory is localhost/mywebsite/
and my CSS file is in css/style.css
localhost/mywebsite/css/style.css
my htaccess
localhost/mywebsite/.htaccess
and my project.php file is in
localhost/mywebsite/project.php
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" />
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.
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"
but when i go live i should change all links to probably something like
href="/css/style.css"
this seems like lots of work
解决方案For your local version add
<base href="//localhost/mywebsite" />
to the head section
and for your live versions change it to
<base href="//your.domain.here" />
reference at http://www.w3.org/TR/html4/struct/links.html#h-12.4
这篇关于URL重写后缺少CSS文件和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!