Forum Moderators: phranque
Condition 1 : Redirect all requests except homepage to other domain.
<VirtualHost>RewriteEngine on
RewriteCond %{REQUEST_URI}!^/$
RewriteCond %{REQUEST_URI}!^/index\.html$
RewriteRule .* http://example.com/ [R=301,L]</VirtualHost>
Condition 2 : Redirect all sub-domain favicon requests to main domain, where all sub-domains are dynamic domains.
<VirtualHost>RewriteEngine on
RewriteCond %{REQUEST_URI} ^/favicon\.ico$
RewriteRule .* /home/example/public_html/favicon.ico [L]</VirtualHost>
Please post your suggestions for improvement.
Milan
Readers please note that this code is written for use in httpd.conf or conf.d, not in .htaccess.
<VirtualHost>
RewriteEngine on
# Externally [b]redirect[/b] all except home page requests to other domain
RewriteRule !^/(index\.html)?$ http://example.com/ [R=301,L]
</VirtualHost>
#
<VirtualHost>
RewriteEngine on
# Internally [b]rewrite[/b] subdomain-subdirectory favicon requests to common/shared directory favicon
RewriteRule ^/([^/]+/)+favicon\.ico$ /home/example/public_html/favicon.ico [L]
</VirtualHost>
Jim
You recommended RewriteLog in some other posts, so I configured it and it was the best debugging I ever need for mod_rewrite.
I thought if we could just trigger rewrite engine when needed by using <Files>.
<VirtualHost><Files "favicon.ico">
RewriteEngine on
RewriteRule ^/([^/]+/)+favicon\.ico$ /home/example/public_html/favicon.ico [L]
</Files></VirtualHost>
But it didn't worked that way and redirected to the following URI
/home/example/public_html/web/home/example/public_html/favicon.ico
while RewriteLog showed correct behavior
[per-dir favicon.ico/] applying pattern '^/([^/]+/)+favicon\.ico$' to uri '/home/example/public_html/web/favicon.ico'
[per-dir favicon.ico/] rewrite /home/example/public_html/web/favicon.ico -> /home/example/public_html/favicon.ico
[per-dir favicon.ico/] internal redirect with /home/example/public_html/favicon.ico [INTERNAL REDIRECT]
Any clues?
Milan
RewriteRule ^/([^/]+/)+favicon\.ico$ /home/example/public_html/favicon.ico [L]
RewriteRule ^/([^/]+/)+favicon\.ico$ /public_html/favicon.ico [L]
Jim
Then I suddenly found this in the RewriteLog
... [per-dir favicon.ico/] …
So, I guess the <Files> directive is supposedly meant for per directory manipulation, adding local URL-path by default.
Milan
The important part of the idea I posted was this: URLs and filepaths are different things, and need not in fact have any relationship to each other; If you use mod_rewrite, you can map /red to /blue, or /big to /small, and no-one the wiser. In order to form a local server filepath from a requested URL, the server does this
Remove the domains name
Apply mod_alias directves
Apply mod_rewrite directives
Add the value of DocumentRoot to the resulting local URL-path (convert it to a local server filepath)
Read the resulting file (or run it as a script) and send the contents to the requesting client
So the main point is that you should not be adding the full server path information to the substitution URL; Add only the part that comes after the DocumentRoot value.
You may also wish to look into the RewriteBase directive in mod_rewrite if you hit a brick wall.
The good news is that once you figure out the correct path information for your particular server's set-up, it will always be the same for all RewriteRules.
Jim
<Directory /home/example/public_html>
Options -Indexes
AddType application/x-httpd-php .php .htm
</Directory><Directory /home/example/public_html/logs>
Deny from All
</Directory><VirtualHost>
ServerName example.in
ServerAlias www.example.in
ServerAdmin webmaster@example.in
DocumentRoot /home/example/public_html
BytesLog domlogs/example.in-bytes_log
CustomLog /usr/local/apache/domlogs/example.in combined
ScriptAlias /cgi-bin/ /home/example/public_html/cgi-bin/User example
Group exampleRedirectMatch 301 /support-(.*) http://example.in/su-$1
RewriteEngine on
RewriteCond %{HTTP_HOST}!^example\.in [NC]
RewriteRule ^/(.*) http://example.in/$1 [R=301,L]
</VirtualHost><VirtualHost>
ServerName web.example.in
ServerAlias *.example.in
ServerAdmin webmaster@web.example.in
DocumentRoot /home/example/public_html/web
BytesLog domlogs/web.example.in-bytes_log
CustomLog /usr/local/apache/domlogs/web.example.in combined
ScriptAlias /cgi-bin/ /home/example/public_html/web/cgi-bin/User example
Group example<Files "favicon.ico">
RewriteEngine on
RewriteRule ^/([^/]+/)+favicon\.ico$ /public_html/favicon.ico [L]
</Files></VirtualHost>
Server processing for h**p://web.example.in/favicon.ico
Apply mod_alias directives : none
Apply mod_rewrite directives :
applying '^/([^/]+/)+favicon\.ico$' to /home/example/public_html/web/favicon.ico -> matched
rewrite /home/example/public_html/web/favicon.ico -> /public_html/favicon.ico
Add the value of DocumentRoot to the resulting local URL-path :
/public_html/favicon.ico -> /home/example/public_html/web/public_html/favicon.ico
This means apache will not let traverse above my DocumentRoot within VirtualHost, or am I still misunderstanding it?
Milan
Yes, that is correct. To do otherwise would be to invite an impossible security-management problem.
If you want to share images across multiple Vhosts, then define an alias "shared" subdirectory in each vhost as a *nix Symbolic Link. Such a link looks like a subdirectory to the user inside that vhost, but is actually a pointer to a directory outside his vhost.
You could also try defining the favicon directory as a server-wide Alias directory -- See Apache mod_alias.
In both cases. make sure you are the only user with write permission on the real directory.
Jim