web-dev-qa-db-fra.com

Réécrire pour robots.txt et favicon.ico

J'ai configuré certaines règles dans lesquelles les sous-domaines (mes utilisateurs) seront définis par défaut à l'endroit où j'ai localisé le fichier robots.txt, favicon.ico et crossdomain.xml.

par conséquent, si un utilisateur crée un site, dites

testing.mywebsite.com et ils ne font pas leur propre favicon.ico à testing.mywebsite.com/favicon.ico, alors il utilisera le favicon.ico que j'ai dans /misc/favicon.ico

Cela fonctionne parfaitement, mais cela ne fonctionne pas pour le site Web principal. Si vous essayez d'aller à mywebsite.com/favicon.ico, il vérifiera si "/" existe, ce qui est le cas. Et puis ne redirige jamais vers /misc/favicon.ico

Comment puis-je l'obtenir afin que les deux instances soient redirigées vers /misc/favicon.ico?

    # Set all crossdomain (openxxx file) favorite icons and robots.txt doesnt exist on their
    # side, then redirect to site's just to have something to go on.
RewriteCond     %{REQUEST_URI}          crossdomain.xml$
RewriteCond     ^(.+)crossdomain.xml    !-f
RewriteRule     ^(.*)$                  /misc/crossdomain.xml [L]

RewriteCond     %{REQUEST_URI}          favicon.ico$
RewriteCond     ^(.+)favicon.ico        !-f 
RewriteRule     ^(.*)$                  /misc/favicon.ico [L]

RewriteCond     %{REQUEST_URI}          robots.txt$
RewriteCond     ^(.+)robots.txt         !-f 
RewriteRule     ^(.*)$                  /misc/robots.txt [L]

Modifier:

Voici mon Vhost complet si cela aide à diagnostiquer:

<VirtualHost *>
        ServerName www.mysite.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/mysite/
        <Directory /var/www/mysite/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>


        ErrorLog /var/log/Apache2/mysite_error.log
        LogLevel warn

        CustomLog /var/log/Apache2/mysite_access.log combined
        ServerSignature on

        RewriteEngine   on        

        #RewriteLog     "/var/log/Apache2/rewrite.log"
        #RewriteLogLevel 9



        RewriteCond     %{HTTP_Host}            ^www.mysite.com$   [NC]
        RewriteRule     ^(.*)$                  - [L]


        RewriteCond     %{HTTP_Host}            ^mysite.com$   [NC]
        RewriteRule     ^(.*)$                  http://www.mysite.com$1 [NC,L,R=302]


        # If there is a subdomain and the subdomain has a /home/ directory attached to it then
        # rewrite the HTTP_Host into the URI so we can process it. If its /media/, go to their
        # media folder, otherwise to their www folder.

        RewriteCond     %{HTTP_Host}                                 ^(?:www\.)?(.+?)\.mysite\.com$
        RewriteCond     /home/%1/                                    -d
        RewriteRule     ^(.+)                                        %{HTTP_Host}$1

        RewriteRule     ^(?:www\.)?([^.]+?)\.mysite\.com/media/(.*) /home/$1/xxx/media/$2 [L]
        RewriteRule     ^(?:www\.)?([^.]+?)\.mysite\.com/(.*)       /home/$1/www/$2




        # Set all crossdomain (openpalace file) favorite icons and robots.txt doesnt exist on their
        # side, then redirect to mainsite's just to have something to go on.
        RewriteCond     %{REQUEST_URI}          crossdomain.xml$
        RewriteCond     ^(.+)crossdomain.xml    !-f
        RewriteRule     ^(.*)$                  /misc/crossdomain.xml [L]

        RewriteCond     %{REQUEST_URI}          favicon.ico$
        RewriteCond     ^(.+)favicon.ico        !-f 
        RewriteRule     ^(.*)$                  /misc/favicon.ico [L]

        RewriteCond     %{REQUEST_URI}          robots.txt$
        RewriteCond     ^(.+)robots.txt         !-f 
        RewriteRule     ^(.*)$                  /misc/robots.txt [L]




        # Same as above but this is for if the directory doe's not exist. Typically it would be
        # wise to put this into a skip to emulate an if/else but it would also match if the 
        # HTTP_Host was anything. The skip If/Else method only works with 1 IF, 2
        # Don't put this as the last thing, there could be redirects later on that may not have a
        # /home/ dir but are just shortcuts to things, like inbox.mysite.com

        RewriteCond     %{HTTP_Host}                                 ^(?:www\.)?(.+?)\.mysite\.com$
        RewriteCond     /home/%1/                                    !-d        
        RewriteRule     ^(.*)$                                       http://www.mysite.com$1 [R=302]





        #Extract the subdomain (if there is one), domain, and tld. Check id the domain exists in
        #/home/. If it does then rewrite URL to http_Host and then see if it can apply to media.
        # There has to be two pairs of rewriteCond because it only applies to the next rule.

                                                                      #www.   domain .   tld
        RewriteCond     %{HTTP_Host}                                ^(?:.*\.)?([^.]+)\.(?:[^.]+)$
        RewriteCond     /home/%1/                                   -d
        RewriteRule     ^(.+)                                       %{HTTP_Host}$1 [C]
        RewriteRule     ^(?:.*\.)?([^.]+)\.(?:[^.]+?)/media/(.*)$    /home/$1/xxx/media/$2 [L] 

                                                                       #www.   domain .   tld
        RewriteCond     %{HTTP_Host}                                ^(?:.*\.)?([^.]+)\.(?:[^.]+)$
        RewriteCond     /home/%1/                                   -d
        RewriteRule     ^(?:.*\.)?([^.]+)\.(?:[^.]+?)/(.*)$          /home/$1/www/$2 [L] 


</VirtualHost>
3
ParoX

Le problème que je rencontrais n'était en réalité même pas lié à l'endroit où je cherchais ...

C'était le

    RewriteCond     %{HTTP_Host}            ^www.mysite.com$   [NC]
    RewriteRule     ^(.*)$                  - [L]

L'option [L] empêchait en fait le mod_rewrite de traiter n'importe quoi, y compris le code favicon.ico.

Je me suis débarrassé de cette règle et ça a fonctionné à merveille.

1
ParoX

Les directives de cette forme sont du charabia, non?

   RewriteCond     ^(.+)favicon.ico        !-f 

Le premier paramètre est une chaîne interpolée variable, pas une expression régulière, donc elle échouera toujours. Même après avoir supprimé les expressions rationnelles du premier paramètre, vous devez ajouter quelque chose comme une référence arrière ou un préfixe pour un chemin dans lequel rechercher favicon.ico.

Est-ce votre seul coupable?

Certains des RewriteRules ont aussi des conditions fausses:

    RewriteRule     ^(?:www\.)?([^.]+?)\.mysite\.com/media/(.*) ...

ce www facultatif. ne peut jamais être là, puisque RewriteRule dans le contexte de l'hôte virtuel correspond toujours à une chaîne commençant par "/". Cela signifie que la plage correspondra toujours à la barre oblique principale.

De plus, pourquoi "mysite.com" serait-il dans le chemin d'URL avec lequel vous faites la comparaison? Avez-vous besoin d'un autre RewriteCond pour fouiller dans HTTP_HOST?

Tout cela serait probablement un peu plus évident avec un RewriteLog.

1
covener