Forum Moderators: phranque

Message Too Old, No Replies

Can yoodoo that .htaccess voodoo?

Need some help with RewriteCond's

         

Fribble

10:29 pm on Jun 29, 2006 (gmt 0)

10+ Year Member



Hi!

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

jdMorgan

1:29 am on Jun 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The flaw is that the subfolder name is part of the local URL-path, and won't be present in the HTTP_HOST variable. So either incorporate the subfolder test into the RewriteRule itself, or, if more convenient, use the %{REQUEST_URI} variable to test it with an additonal RewriteCond.

Jim

Fribble

4:34 am on Jun 30, 2006 (gmt 0)

10+ Year Member



I usually pick this stuff up fast, but I'm having some real problems forming a foundation for understanding all of the different commands and characters used in all of the different .htaccess's that I'm using as examples. Looks like gibberish to me and apache.org's docs aren't much better :) I just burned up 3 hours trying to figure this thing out.

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!

jdMorgan

6:25 am on Jun 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you're using a sort of 'strange specialized language' here, and a highly-compact, very-powerful one at that. The syntax of both mod_rewrite and the regular-expressions pattern-matching that it depends on are rather obscure. On the other hand, you can get a lot done with just a tiny bit of code.

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]

I have taken the liberty of adding an additional rewritecond (the first line of the first ruleset) to redirect any requests for "addondomain.com" to the canonical "www.addondomain.com" -- You do not want both the www- and non-www versions of any domain name to appear to coexist, as this will trigger duplicate content problems, at best splitting the PageRank and/or link-popularity across both versions. So, in this example, addondomain.com/<whatever> is permanently redirected to www.addondomain.com/<whatever>

For the second ruleset, note that the typical contents of THE_REQUEST might be something like:

GET /addondomain/favicon.ico HTTP/1.1
so 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

Fribble

9:45 pm on Jul 2, 2006 (gmt 0)

10+ Year Member



Wow, thanks for taking the time to put this together for me jd. Unfortunately, I am still met with the same results - addondomain.example.com redirects fine, but example.com/addondomain does not.

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

jdMorgan

8:37 pm on Jul 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> addondomain.example.com redirects fine, but example.com/addondomain does not.

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