Forum Moderators: phranque
Pretty new to mod_rewrite stuff, hence not understanding the syntax fully as from other posts. So decided to ask you experts directly.
With /home/www, as the home directory for my main domain (itself a virtualhost), I'm trying to set sub-domains to point to sub-directories.
eg.
mail.mydomain.com points to /home/www/mail
test1.mydomain.com points to /home/www/testing/test1
test2.mydomain.com points to /home/www/testing/test2
I'm trying to find out what to add into htaccess file to make this work. Where should the htaccess file reside? on /home/www? Or on each sub directory with a different rewrite condition?
Thanks.
Have the following relevant info in httpd.conf :
# mydomain.com
<VirtualHost NNN.NNN.NNN.NNN>
DocumentRoot /home/www
ServerName www.mydomain.com
UseCanonicalName off
RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond %{HTTP_HOST}!^(.*)mydomain.com
RewriteCond %{HTTP_HOST}!^NNN.NNN.NNN.NNN.*
RewriteRule ^/(.*)$ /home/www/${lowercase:%{HTTP_HOST}}/$1
RewriteRule ^/home/www/www\.(.*)$ /home/www/$1
RewriteCond %{REQUEST_URI} ^/cgi-bin/.*
RewriteCond %{HTTP_HOST}!^(.*)mydomain.com
RewriteCond %{HTTP_HOST}!^NNN.NNN.NNN.NNN.*
RewriteRule ^/(.*)$ /home/www/${lowercase:%{HTTP_HOST}}/$1
[T=application/x-httpd-cgi]
RewriteRule ^/usr/www/www\.(.*)$ /home/www/$1
[T=application/x-httpd-cgi]
</VirtualHost>
As stated in our charter, we prefer not to write your code for you, but to help you write your own. Here's an example off one of my sites that rewrites requests for http://test.example.com/ to the subdirectory /test on the server. This code is installed in .htaccess in the web root directory of the site.
RewriteEngine on
# Redirect test subdomain to subdirectory /test
RewriteCond %{HTTP_HOST} ^test\.example\.com
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule (.*) /test/$1 [L]
Jim
Just a note that the httpd.conf, is not modifiable by me. My host is a defined virtual host in the httpd.conf using an IP.
Better yet, try a simple test. Place the following in your .htaccess file in the web root directory on your server (where your "home page" goes):
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^silly\.html$ /index.html [L]
If this does not work, then your server is probably not configured to allow mod_rewrite.
Jim
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^silly\.html$ /test5/index.html [L]
I'm still thinking its some condition/rule in httpd.conf that is blocking this. Just not sure which one might be causing some conflict.
My directory structure is as follows :
[/usr/home/myhome] % ls -l www
lrwxr-x--x 1 myhome 84 36 Nov 15 21:17 www -> /usr/home/myhome/mydomain.com/htdocs
[/usr/home/myhome] % ls -l mydomain.com
drwxr-xr-x 11 myhome 84 512 Nov 15 10:54 htdocs
If the code worked with Options +FollowSymLinks placed *after* RewriteEngine on, then you do not need Options +FollowSymLinks at all. If you had needed it, the RewriteEngine on directive would have caused an error, because FollowSymLinks is needed to 'enable' the rewrite engine. FollowSymLinks is enabled by default in httpd.conf, otherwise it would not have worked.
You haven't really said what happens when you try to do the rewrite. I would recommend looking at the raw error log if you get any errors. You may need to use the RewriteBase directive if your server seems to be looking in the wrong path for the substituted directories and files.
Sometime it's even useful to force an error - like a 404 - to see what shows up in the error log. Redirect to a file you *know* does not exist, and then check the error log.
Your httpd.conf code apparently allows arbitrary subdomain names (unless I'm missing something), so I don't see why httpd.conf would be interfering with the redirect.
Jim
RewriteEngine on
# Redirect test subdomain to subdirectory /test
RewriteCond %{HTTP_HOST} ^test\.example\.com
RewriteCond %{REQUEST_URI}!^/test/
RewriteRule (.*) /test/$1 [L]
lets say now I have other subdomains, will I have to repeat these same 3 lines for each subdomain?
Thanks once again.
You can *try* this - I didn't test it. %1 is used as a back-reference to pick up and re-use the subdomain name from the parenthesized subexpression of the second RewriteCond:
RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www'
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule (.*) /%1/$1 [L]
RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www'
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST}!^mail\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteCond %{REQUEST_URI}!^/%1/
RewriteRule (.*) /mydata/%1/$1 [L]
What I am trying to do is to redirect all canonical names like 'abc.example.com' to 'www.example.com/mydata/abc', excluding 'www.*' and 'mail.*'.
But its erroring in the log that MaxReDirects has been exceeded. RewriteOptions MaxReDirects is set to 20 currently. Seems a loop is occurring?
Add a RewriteCond to stop it:
RewriteCond %{REQUEST_URI} !^/mydata/
Jim