Forum Moderators: phranque

Message Too Old, No Replies

Rewrite issue with subdirectory forwarding to subdomain

         

JPigford

4:58 am on Nov 10, 2007 (gmt 0)

10+ Year Member



I currently have the following:

http://www.example.com/affiliate/

There are hundreds of links using that subdirectory (like example.com/affiliate/user.php?123) and I need everything to forward now to:

[affiliate.example.com...] (or whatever the link may be)

I do not need every subdirectory to do this. I need only the /affiliate/ directory.

I tried this but all browsers are timing out saying it's endlessly redirecting.

RewriteRule ^affiliate(.*)$ http://affiliate.example.com$1 [R=301]

Any ideas?

jdMorgan

2:35 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have another rule or rules that perform the reverse function, internally mapping the subdomain to that subdirectory? If so, the two rules will repeatedly countermand each other, leading to a loop.

If this is the case, you'll need to use a RewriteCond testing THE_REQUEST so that external redirection only takes place if the client directly requests the subdirectory, and not if the subdirectory is requested as the result of an internal rewrite of the subdomain URL.

Jim

JPigford

4:09 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



This is what I currently have in my htaccess. The first rule obviously is to force the www. on the URL, but I can't seem to get it to play nice with the affiliate subdomain.


RewriteCond %{HTTP_HOST}!^www.example.com$
RewriteRule ^/?(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^/?affiliate/(.*)$ http://affiliate.example.com/$1 [R=301,L]

jdMorgan

4:36 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, your first rule will conflict with the second, because it redirects any non-www request to www. SInce "affiliate" is not "www" the rules conflict. Two ways to fix it:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^affiliate\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^affiliate/(.*)$ http://affiliate.example.com/$1 [R=301,L]

or equivalently

RewriteCond %{HTTP_HOST} !^(www¦affiliate)\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^affiliate/(.*)$ http://affiliate.example.com/$1 [R=301,L]

Replace the broken pipe "¦" character with a solid pipe before use; Posting on this forum modifies the pipe characters.

Do not end-anchor hostname patterns, unless you account for a (valid) port name appended to the hostname in the request header. Otherwise, any client that does append a port number will 'break' your RewriteRules. If you feel you must end-anchor the hostname patterns, then use something like any one of these:


RewriteCond %{HTTP_HOST} !^www\.example\.com(:80)?$
RewriteCond %{HTTP_HOST} !^www\.example\.com(:80¦443)?$
RewriteCond %{HTTP_HOST} !^www\.example\.com(:[0-9]+)?$

Jim

JPigford

6:49 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



It's still endlessly redirecting.

I type in something like:

http://www.example.com/affiliate/account.php

it then tries to forward to:

[affiliate.example.com...]

then it just forwards right back to:

http://www.example.com/affiliate/account.php

And then the browser times out saying there are too many redirects.

For what it's worth, I'm running Apache 1.3.37.

[edited by: JPigford at 6:53 pm (utc) on Nov. 10, 2007]

JPigford

6:57 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



Also, if I drop those first two rules and only have the following, it still endlessly redirects...so seems there might be some issue with the last rule:


RewriteRule ^affiliate/(.*)$ http://affiliate.example.com/$1 [R=301,L]

jdMorgan

9:08 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What procedure did you use to 'create' this subdomain and its subdirectory?

If you used a 'control panel' then it's likely that it created a rewriterule in the server config file that is conflicting with your existing rule as described above. Also, some control panels will show you the config file they create -- Is this information accessible to you?

Jim

JPigford

9:12 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



I've got full root access to the server. Where might I find this info and what exactly would I be looking for?

The server is running cPanel/WHM on the front end, but like I said, I've got access to things like httpd.conf if necessary.

jdMorgan

9:14 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



look in httpd.conf and conf.d -- those are the most likely files.

All we need to do is find the code that the server is using to map afilliate.example.com to /affiliate. Given that code and the code you're trying to add, it should be easy to make them play nice together.

Jim

JPigford

9:41 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



Not sure what I'm looking for, but here is the VirtualHost entry:


<VirtualHost XX.XX.XX.XX>
ServerAlias www.affiliate.example.com
ServerAdmin webmaster@affiliate.example.com
DocumentRoot /home/user/public_html/affiliate
BytesLog domlogs/affiliate.example.com-bytes_log
User user
Group user
ServerName affiliate.example.com

User user
Group user
CustomLog /usr/local/apache/domlogs/affiliate.example.com combined
ScriptAlias /cgi-bin/ /home/user/public_html/affiliate/cgi-bin/
</VirtualHost>

jdMorgan

9:55 pm on Nov 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It doesn't seem like you should have this problem, but try this:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /affiliate/
RewriteRule ^affiliate/(.*)$ http://affiliate.example.com/$1 [R=301,L]

This will suppress the redirect if '/affiliate/ is requested as the result of an internal request, but invoke it if the request comes from a client.

Jim

[edited by: jdMorgan at 9:55 pm (utc) on Nov. 10, 2007]

JPigford

10:13 pm on Nov 10, 2007 (gmt 0)

10+ Year Member



Looks like that did it!

Thanks so much for help Jim!