: Rewrite for robots.txt and favicon.ico I have setup some rules in which subdomains (my users) will default to where I have located the robots.txt, favicon.ico, and crossdomain.xml therefore if
I have setup some rules in which subdomains (my users) will default to where I have located the robots.txt, favicon.ico, and crossdomain.xml
therefore if a user creates a site say
testing.mywebsite.com and they don't make their own favicon.ico at testing.mywebsite.com/favicon.ico, then it will use the favicon.ico I have in /misc/favicon.ico
This works perfect, but it doesn't work for the main website. If you attempt to go to
mywebsite.com/favicon.ico it will check if "/" exists, in which it does. And then never redirects to /misc/favicon.ico
How can I get it so both instances redirect to /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]
Edit:
Here is my full Vhost if it helps in diagnosing:
<VirtualHost *>
ServerName mysite.com ServerAdmin webmaster@mysite.com
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 ^(.*)$ www.mysite.com [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}
RewriteRule ^(?:www.)?([^.]+?).mysite.com/media/(.*) /home//xxx/media/ [L]
RewriteRule ^(?:www.)?([^.]+?).mysite.com/(.*) /home//www/
# 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 ^(.*)$ www.mysite.com [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} [C]
RewriteRule ^(?:.*.)?([^.]+).(?:[^.]+?)/media/(.*)$ /home//xxx/media/ [L]
#www. domain . tld
RewriteCond %{HTTP_HOST} ^(?:.*.)?([^.]+).(?:[^.]+)$
RewriteCond /home/%1/ -d
RewriteRule ^(?:.*.)?([^.]+).(?:[^.]+?)/(.*)$ /home//www/ [L]
</VirtualHost>
More posts by @Connie744
2 Comments
Sorted by latest first Latest Oldest Best
The problem I was having was actually not even related to where I was looking...
It was the
RewriteCond %{HTTP_HOST} ^www.mysite.com$ [NC]
RewriteRule ^(.*)$ - [L]
The [L] option actually prevented the mod_rewrite from even processing anything beflow, in which where the favicon.ico code was at.
I got rid of that rule&cond and it worked like a charm.
Directives of this form are gibberish, right?
RewriteCond ^(.+)favicon.ico !-f
The first parameter is a variable interpolated string, not a regex, so it will always fail. Even after removing the regex from the first parm, you need to add in something like a backreference or a prefix for a path to look for favicon.ico in.
Is that your only culprit?
Some of the RewriteRules also have bogus conditions baked into them:
RewriteRule ^(?:www.)?([^.]+?).mysite.com/media/(.*) ...
that optional can never be there, since RewriteRule in virtual host context is always matching against a string starting with "/". This means the range will always match the leading slash.
Additionally, why would "mysite.com" be in the URL-path you're matching against? Do you ned another RewriteCond to poke around in HTTP_HOST?
This would all probably be a bit more apparent with a RewriteLog.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.