php .htaccess 不包括重写 css 和图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8864821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.htaccess excluding rewrite css and images
提问by Miroslav
I have some problems with rewriting URL-s
我在重写 URL-s 时遇到了一些问题
my .htacces file:
我的 .htaccess 文件:
RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)$ index.php?url= [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:
比我的 css 工作或者我可以开始工作。但是当我有下一个链接时:
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
我无法让 css 工作,css 被重定向到
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?
现在定义的重写规则适用于不同的链接,但如何为 css 和图像添加排除规则?
I get next .htaccess that work for css for all my conditions:
我得到下一个 .htaccess 适用于我所有条件的 css:
RewriteEngine On
RewriteBase /bsc/
RewriteCond %{REQUEST_FILENAME} !\.css
RewriteRule ^(.+/?)$ index.php?url= [NC,QSA,L]
After reading comments and some time I get next .htaccess file with not so bad results:
在阅读评论和一段时间后,我得到了下一个 .htaccess 文件,结果还不错:
RewriteEngine On
RewriteBase /bsc/
RewriteRule ^(.+/?)?(/|\.htm|\.php|\.html|/[^.]*)$ index.php?url= [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:
现在,当我无法获得正确的 URL 请求时,它只是 URL 中的一种条件。下一个 URL 不接收最后一个参数:
localhost/bsc/page/subpage
localhost/bsc/page/subpage/subsubpage
I don't receive subpageor subsubpage, other conditions are working. In this situation apache and .htaccess tray to find file or folders ....
我没有收到子页面或subsubpage,其他的条件下工作。在这种情况下 apache 和 .htaccess 托盘来查找文件或文件夹....
Is it possible to add missing condition or is it possible to make some simplest rewrite rule or more rules
是否可以添加缺失条件或者是否可以制定一些最简单的重写规则或更多规则
回答by Tehnix
You probably just need to use an absolute url for you css,
你可能只需要为你的 css 使用一个绝对 url,
i.e.
IE
<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.
将斜杠放在前面会强制将其置于 url 的“根”,从而使其在整个子页面中保持一致。
回答by Sajal
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
我将 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]
回答by Wrikken
- Add the condition
RewriteCond %{REQUEST_FILENAME} !-f
before your rewriterule. - You should include css with
src="/css/style.css"
and NOTsrc="css/style.css"
(note the missing starting/
.
RewriteCond %{REQUEST_FILENAME} !-f
在重写规则之前添加条件。- 您应该包含 css with
src="/css/style.css"
和 NOTsrc="css/style.css"
(注意缺少的开头/
.