.htaccess 不包括重写 CSS 和图像
我在重写 URL-s 我的 .htacces 文件时遇到一些问题
:
RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]
我的文件和文件夹结构:
bsc/
---|.htaccess
---|index.php
---|css/
-------|style.css
---|images/
----------|logo.png
----------|image1.jpg
----------|image2.jpg
---|controller/
--------------|bootstrap.php
...
当我有下一个链接时
localhost/bsc/page.html
,我的 css 工作或者我可以开始工作。 但是当我有下一个链接时:
localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage.html
localhost/bsc/page/subpage/subsubpage/subsubsubpage.html
我无法让 css 工作,css 被重定向到
localhost/bsc/page/css/style.css
localhost/bsc/page/subpage/css/style.css
localhost/bsc/page/subpage/subsubpage/css/style.css
问题的第二部分是我的链接没有相同的扩展名或任何扩展名。 接下来的情况是可能的:
localhost/bsc/page/subpage
localhost/bsc/page/subpage/
localhost/bsc/page/subpage.php
localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage
localhost/bsc/page/subpage/subsubpage/
localhost/bsc/page/subpage/subsubpage.php
localhost/bsc/page/subpage/subsubpage.html
...
现在定义的重写规则适用于不同的链接,但如何为 css 和图像添加排除规则?
我得到下一个 .htaccess,它适用于我所有条件下的 css:
RewriteEngine On
RewriteBase /bsc/
RewriteCond %{REQUEST_FILENAME} !\.css
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]
在阅读评论和一段时间后,我得到下一个 .htaccess 文件,结果还不错:
RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)?(/|\.htm|\.php|\.html|/[^.]*)$ index.php?url=$1 [NC,QSA,L]
我的链接标记是:
<link rel="stylesheet" type="text/css" href="/bsc/css/style.css" />
现在它只是 URL 中的一个条件,当我可以时无法获得正确的 URL 请求。下一个网址未收到最后一个参数:
localhost/bsc/page/subpage
localhost/bsc/page/subpage/subsubpage
我未收到子页面或子子页面,其他条件有效。在这种情况下,apache和.htaccess托盘查找文件或文件夹....
是否可以添加缺少的条件,或者是否可以制定一些最简单的重写规则或更多规则
I have some problems with rewriting URL-s
my .htacces file:
RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]
my file and folder structure:
bsc/
---|.htaccess
---|index.php
---|css/
-------|style.css
---|images/
----------|logo.png
----------|image1.jpg
----------|image2.jpg
---|controller/
--------------|bootstrap.php
...
When I have next link
localhost/bsc/page.html
Than my css work or I can get to work.
But when I have next links:
localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage.html
localhost/bsc/page/subpage/subsubpage/subsubsubpage.html
I can't get css to work, css is redirected to
localhost/bsc/page/css/style.css
localhost/bsc/page/subpage/css/style.css
localhost/bsc/page/subpage/subsubpage/css/style.css
Second part of problem is that my links don't have same extensions or any extension.
Next situations are possible:
localhost/bsc/page/subpage
localhost/bsc/page/subpage/
localhost/bsc/page/subpage.php
localhost/bsc/page/subpage.html
localhost/bsc/page/subpage/subsubpage
localhost/bsc/page/subpage/subsubpage/
localhost/bsc/page/subpage/subsubpage.php
localhost/bsc/page/subpage/subsubpage.html
...
Now defined rewrite rule is working for different links but how to add excluded rule for css and images?
I get next .htaccess that work for css for all my conditions:
RewriteEngine On
RewriteBase /bsc/
RewriteCond %{REQUEST_FILENAME} !\.css
RewriteRule ^(.+/?)$ index.php?url=$1 [NC,QSA,L]
After reading comments and some time I get next .htaccess file with not so bad results:
RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)?(/|\.htm|\.php|\.html|/[^.]*)$ index.php?url=$1 [NC,QSA,L]
and my link tag is:
<link rel="stylesheet" type="text/css" href="/bsc/css/style.css" />
Now it is only one condition in URL when I can't get correct URL request. Next URL don't receive last parameter:
localhost/bsc/page/subpage
localhost/bsc/page/subpage/subsubpage
I don't receive subpage or subsubpage, other conditions are working. In this situation apache and .htaccess tray to find file or folders ....
Is it possible to add missing condition or is it possible to make some simplest rewrite rule or more rules
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能只需要为 css 使用绝对 url,
即
不要
在前面放置斜杠,而是将其强制到 url 的“根”,从而使其在整个子页面中保持一致。
You probably just need to use an absolute url for you css,
i.e.
instead of
Putting a slash infront forces it to the "root" of the url, thus making it consistent throughout subpages.
我将 url 重写应用于核心 php 中的一个项目。我遇到了同样的问题。 css、Js 和图像未加载。我用过这个,它对我有用。 来源
I applied url rewriting to a project in core php. I faced the same problem. css, Js and images were not getting loaded. I used this and it worked for me. Source
RewriteCond %{REQUEST_FILENAME} !-f
。src="/css/style.css"
而不是src="css/style.css"
(注意缺少起始/< /代码>。
RewriteCond %{REQUEST_FILENAME} !-f
before your rewriterule.src="/css/style.css"
and NOTsrc="css/style.css"
(note the missing starting/
.