.htaccess 将文件路径 favicon.ico 重写为 core/favicon.ico

huangapple go评论54阅读模式
英文:

.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

&lt;IfModule mod_rewrite.c&gt;
	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]
&lt;/IfModule&gt;

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").

huangapple
  • 本文由 发表于 2023年3月31日 21:58:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定