Forum Moderators: phranque
I've used CPanel to add several "Add-on" domains into an account, which seems to set them up so they can be accessed by any of the following methods:
[maindomain.com...]
[addondomain.maindomain.com...]
[addondomain.com...]
I am trying to write an .htaccess that I can place into the addon domain's folder that will 301 redirect the first two access methods to the [addondomain.com...] address.
I've discovered that the following RewriteCond's work to redirect the subdomain:
RewriteCond %{HTTP_HOST} ^addondomain\.maindomain\.com
RewriteCond %{HTTP_HOST} ^www\.addondomain\.maindomain\.com
But I can't seem to find the right ReqriteCond's to tag the subfolder. I've tried the following 4 combinations without success:
RewriteCond %{HTTP_HOST} ^maindomain\.com/addondomain
RewriteCond %{HTTP_HOST} ^maindomain\.com/addondomain/
RewriteCond %{HTTP_HOST} ^maindomain\.com/
RewriteCond %{HTTP_HOST} ^maindomain\.com
If anyone can take a moment to point out the flaw(s) here I would be grateful.
Thanks,
Fribs
I'll put this on hold for now and put a few hours aside this weekend for a crash course. This is something I need to understand better anyway.
I appreciate the help jd. Would you recommend reading all of Apache.org's docs or is there another resource you know of that can better laymanize this for us .htnewbies?
Thanks!
Our forum charter [webmasterworld.com] contains links to several useful resources, and there are some good tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
If I understand your problem, here's one way to address it:
# If request is for addondomain.com/<anything>
RewriteCond %{HTTP_HOST} ^addondomain\.com [OR]
# OR If request is for addondomain.example.com/<anything> or www.addondomain.example.com/<anything>
RewriteCond %{HTTP_HOST} ^(www\.)?addondomain\.example\.com
# Then redirect to www.addondomain.com/<anything>
RewriteRule (.*) http://www.addondomain.com/$1 [R=301,L]
#
# If client request is for example.com/addondomain/<anything> or www.example.com/addondomain/<anything>
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /addondomain
# Then redirect to www.addondomain/<anything>
RewriteRule ^addondomain(.*) http://www.addondomain.com$1 [R=301,L]
For the second ruleset, note that the typical contents of THE_REQUEST might be something like:
GET /addondomain/favicon.ico HTTP/1.1so the regex I used will match GET, POST, HEAD, etc, followed by a space, and then /addondomain and anything else that follows. The more-complex second ruleset uses the additional and seemingly-redundant rewritecond testing THE_REQUEST specifically because that variable is not affected by any rewrites or redirects that your cPanel code might also be doing to 'map' the subdomain to a subdirectory. By using this non-updating variable containing the original client request, a potential infinite redirection loop is avoided.
Note that if the code produced by your cPanel uses an external redirect instead of a server-internal rewrite to 'map' addondomain.com to maindomain.com/subfolder, then that second ruleset won't work -- you'll get an infinite redirection loop as the cPanel code and the second ruleset redirect back and forth until either the server or your browser gives up and declares an error. However, although cPanel does occasionally 'write' some sloppy code, I don't think they make that particular mistake.
Most rewrites and redirects are not this involved; You just happened to pick a rather complex and multi-faceted problem to start with... :)
Jim
I've managed to dig up its entry in my hppd.conf:
<VirtualHost 123.12.123.123>
ServerAlias www.addondomain.example.com
ServerAdmin webmaster@addondomain.example.com
DocumentRoot /home/username/public_html/addondomain
BytesLog domlogs/addondomain.example.com-bytes_log
ServerName addondomain.example.com
ServerAlias addondomain.com www.addondomain.com
UseCanonicalName off
<IfModule mod_php4.c>
php_admin_value open_basedir "/home/username:/usr/lib/php:/usr/local/lib/php:/tmp"
</IfModule>
<IfModule mod_php5.c>
php_admin_value open_basedir "/home/username:/usr/lib/php:/usr/local/lib/php:/tmp"
</IfModule>
User username
Group username
CustomLog /usr/local/apache/domlogs/addondomain.example.com combined
ScriptAlias /cgi-bin/ /home/username/public_html/addondomain/cgi-bin/
</VirtualHost>
Once again referencing Apache.org's vhosts docs, I don't see anything obvious that I can tweak to deny access to the folder (But it does look like I can eliminate the subdomain) and I'm veryhesitant to just start experimenting with this file on a live server. Does this provide any insight into what direction I need to search?
I will begin my research here shortly, thanks for the links - I always forget about the forum libraries! :) Once I find the answer I will post if for any searchers that find this thread.
Fribs
The code for redirecting example.com/addondomain.example.com needs to be located in httpd.conf, conf.d, or .htaccess for the example.com virtual server. If placed in the addondomain.example.com virtual serve, it will never be accessed, and so can have no effect.
I had assumed that all of your domains and subdomains mapped to the same virtual server, which is apparently not the case.
It's all a matter of putting each rule where it will actually be executed on a request. It is possible you may need to modify or even duplicate the rules for your various virtual hosts.
Jim