Rewrite rule help in apache 2.
Date: 12/09/04
(Apache) Keywords: php, web, hosting
We are using dynamic virtual hosting for a number of domains. The original request was to be able to go to http://somedomain.com/dav and update a webpage. This worked great. But now we would like to do this with dav over ssl. We don't want to buy an ssl certificate for every domain, so I would like to do a redirect from the virtual host to the ssl host. I can not do a Redirect /dav https://sslhost.com/davvhosts/%O as Redirect can not handle the %O.
So I would like to do a Rewrite on the ssl server based on the HTTP_REFERER variable, but I can't get it to work:
The Virtual host stuff:
UseCanonicalName Off
CustomLog logs/access_log vcommon
VirtualDocumentRoot /web/vhosts/%0/htdocs
VirtualScriptAlias /web/vhosts/%0/cgi-bin
Options FollowSymLinks ExecCGI Multiviews Includes
AllowOverride All
ForceType application/x-httpd-php
Redirect /dav https://www.sightworks.net/davvhosts
Then in the SSL side I tried a couple things. First just modifying the HTTP_REFERER variable:
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond %{HTTP_REFERER} /dav/
RewriteRule ^http://(.*)/dav/ $1
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond %{REQUEST_URI} ^/davvhosts/
RewriteRule ^/(.*)$ /www/vhosts/${lowercase:%{HTTP_REFERER}}/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteRule ^/(.*)$ /www/vhosts/${lowercase:%{HTTP_REFERER}}/cgi-bin/$1 [T=application/x-httpd-cgi]
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond %{REQUEST_URI} ^/davhosts/
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{HTTP_REFERER}}/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/
I also tried just setting a different variable and using it, but that did not work either:
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond %{HTTP_REFERER} /dav/
RewriteRule ^http://(.*)/dav/ - [E=HTTP_REFERER_HOST:$1]
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond %{REQUEST_URI} ^/davvhosts/
RewriteRule ^/(.*)$ /www/vhosts/${lowercase:%{HTTP_REFERER_HOST}}/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/
RewriteRule ^/(.*)$ /www/vhosts/${lowercase:%{HTTP_REFERER_HOST}}/cgi-bin/$1 [T=application/x-httpd-cgi]
RewriteCond %{REQUEST_URI} !^/icons/
RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond %{REQUEST_URI} ^/davhosts/
RewriteRule ^/(.*)$ /www/hosts/${lowercase:%{HTTP_REFERER_HOST}}/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/
In both cases it looks like the variable being set ends up blank.
Is what I want to do possible? If so, what am I doing wrong?
CANCEL THAT QUESTION! I figured out the answer:
RewriteEngine on
RewriteLog /web/etc/httpd/logs/rewrite.log
RewriteCond %{REQUEST_URI} ^/dav$ [OR]
RewriteCond %{REQUEST_URI} ^/dav/
RewriteCond %{HTTP_HOST} !^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
RewriteRule ^/dav.*$ https://www.sslhost.net/davvhosts/%{HTTP_HOST}/ [R,L]
Source: http://www.livejournal.com/community/apache/14489.html