A
A
Alexander Neverov2020-07-14 12:25:19
htaccess
Alexander Neverov, 2020-07-14 12:25:19

How to properly .htaccess "RewriteRule ^ %1/favicon.ico[L]"?

I do not understand why one conversion does not work.

There is .htaccess text below.
The only thing that doesn't work is:

RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
  RewriteCond %{REQUEST_URI} ^/favicon.ico$
  RewriteRule ^ %1/favicon.ico [L]

It doesn't seem to see the contents of %1, although the similar lines below on "Public file" and "PHP script" work as intended.

those. it should take the favicon.ico from the appropriate folder, for example blog/favicon.ico. And it always takes from the root.

# Задает кодировку текста
    AddDefaultCharset utf-8
# Отключаем слэш на директории
  DirectorySlash Off
# Включаем работу механизмов преоброзования
    RewriteEngine On
    Options +SymLinksIfOwnerMatch
# Устанавливает базовый URL
  RewriteBase /

# Останов зацикливания RewriteRule
  RewriteCond %{ENV:REDIRECT_STATUS} .
  RewriteRule ^ - [L]

#-------- robot.txt --------#
  RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
  RewriteCond %{REQUEST_URI} ^/robot.txt$
  RewriteRule ^ robot.txt [L]
#-------- favicon.ico --------#
  RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
  RewriteCond %{REQUEST_URI} ^/favicon.ico$
  RewriteRule ^ %1/favicon.ico [L]
  #RewriteRule ^ project/favicon.ico [L]

#-------- Public file --------#
  RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
  RewriteCond %{DOCUMENT_ROOT}/%1/$1 -f
  RewriteRule ^(resources/public.*)$ %1/$1 [L]

#-------- URL slash --------#
  RewriteCond %{REQUEST_URI} ^(.+)/$
  RewriteRule ^(.+)/$ https://%{SERVER_NAME}/$1 [R=301,L]
#-------- URL HTTPS --------#
  RewriteCond %{HTTP:SSL} !=1 [NC]
  RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [L,R=301]

#-------- PHP script --------#
  RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
  RewriteRule ^ %1/index.php [L]

#-------- THE END !!! --------#
  RewriteRule ^ - [R=404,L]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-07-14
@NeverovWork

RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
RewriteCond %{REQUEST_URI} ^/favicon.ico$
RewriteRule ^ %1/favicon.ico [L]

Every time a regular expression matches successfully, the variables %1-%9 are overwritten in RewriteCond.
Matching ^/favicon.ico$ overwrites the previous result in %1.
The conditions just need to be reversed.
RewriteCond %{REQUEST_URI} ^/favicon.ico$
RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
RewriteRule ^ %1/favicon.ico [L]

Or use a regular string comparison, not a regular expression.
RewriteCond %{SERVER_NAME} ^(www|blog|project|xerox)\.*
RewriteCond %{REQUEST_URI} =/favicon.ico
RewriteRule ^ %1/favicon.ico [L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question