.htaccess 不包括重写 CSS 和图像

发布于 2024-12-27 04:37:50 字数 2033 浏览 1 评论 0原文

我在重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城之時有危險 2025-01-03 04:37:50

您可能只需要为 css 使用绝对 url,

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

不要

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

在前面放置斜杠,而是将其强制到 url 的“根”,从而使其在整个子页面中保持一致。

You probably just need to use an absolute url for you css,

i.e.

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

instead of

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

Putting a slash infront forces it to the "root" of the url, thus making it consistent throughout subpages.

过潦 2025-01-03 04:37:50

我将 url 重写应用于核心 php 中的一个项目。我遇到了同样的问题。 css、Js 和图像未加载。我用过这个,它对我有用。 来源

# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

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

# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
茶花眉 2025-01-03 04:37:50
  1. 在重写规则之前添加条件 RewriteCond %{REQUEST_FILENAME} !-f
  2. 您应该包含 css 与 src="/css/style.css" 而不是 src="css/style.css" (注意缺少起始 /< /代码>。
  1. Add the condition RewriteCond %{REQUEST_FILENAME} !-f before your rewriterule.
  2. You should include css with src="/css/style.css" and NOT src="css/style.css" (note the missing starting /.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文