英文:
.htaccess rewrite file path favicon.ico to core/favicon.ico
问题
I am doing something wrong with writing my .htaccess
syntax and what exactly I am unsure.
I have a file called /core/favicon.ico
.
Now I want when the browser requests /favicon.ico
it to have the server display: /core/favicon.ico
.
The contents of my .htaccess
is currently:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
RewriteRule ^favicon.ico core/favicon.ico [NC,L]
</IfModule>
The previous rewrite rule is needed for the functioning of my system.
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
And that rule works fine.
英文:
I am doing something wrong with writing my .htaccess
syntax and what exactly I am unsure.
I have a file called /core/favicon.ico
.
Now I want when the browser requests /favicon.ico
it to have the server display: /core/favicon.ico
.
The contents of my .htaccess
is currently
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
RewriteRule ^favicon.ico core/favicon.ico [NC,L]
</IfModule>
The previous rewrite rule is needed for the functioning of my system.
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
And that rule works fine.
答案1
得分: 1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
RewriteRule ^favicon.ico core/favicon.ico [NC,L]
你基本上只是把规则放反了。可能 /favicon.ico
不存在,所以第一个规则首先将请求重写为 core/index.php?url=favicon.ico
,然后处理停止。
你需要颠倒这两个规则(并反斜杠转义点并添加字符串末尾锚点)。例如:
RewriteRule ^favicon.ico$ core/favicon.ico [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
另外,这不会重写对于“主页”(文档根目录)的请求,所以我假设你有其他处理它的东西?否则,我期望你需要在文件顶部相应地设置 DirectoryIndex
。例如:
DirectoryIndex /core/index.php
请注意,对于这种请求(对于“主页”),url
参数将不存在。
英文:
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
>
> RewriteRule ^favicon.ico core/favicon.ico [NC,L]
You've basically just got your rules in the wrong order. Presumably /favicon.ico
does not exist, so the first rule rewrites the request to core/index.php?url=favicon.ico
first and processing stops.
You need to reverse the two rules (and backslash-escape the dot and add the end-of-string anchor). For example:
RewriteRule ^favicon\.ico$ core/favicon.ico [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
Aside: This does not rewrite requests for the "homepage" (the document root), so presumably you have something else that handles that? Otherwise, I would expect you need to set the DirectoryIndex
accordingly at the top of the file. For example:
DirectoryIndex /core/index.php
Note that the url
parameter will be absent for such requests (to the "homepage").
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论