Forum Moderators: phranque

Message Too Old, No Replies

subdomain redirecting to main domain

         

scottrmay

8:46 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Happy holidays to all,

I have not seen a solution to my problem. I want all subdomains to be redirected to my home page. I have tried several different codes in an htaccess file, none of them work.

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

I would think this one would work, but it doesn't and I am not smart enough to figure out why.

If anyone could help me, I sure would appreciate it. I have many many hours in this so far and all I've gotten out of it so far is boney fingers. :-)

Thanks in advance and peace to you all.

Scott

[edited by: jdMorgan at 12:31 am (utc) on Dec. 31, 2003]
[edit reason] examplified, de-linked [/edit]

jdMorgan

9:43 pm on Dec 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Scott,

It looks like

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

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

Other than that, your code looks fine.

%1 refers to the first grouped subpattern in the last RewriteCond that matched. In your code above, that would be the name of the requested subdomain matched by "(.+)". $1 refers to the first grouped subpattern in the RewriteRule itself, in your case, the requested page.

If it is the case that you want to refer to the requested page, then you can remove a few 'extras' that won't be needed:


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

As an aside, the use of parentheses in mod_rewrite regular expressions does two things: First it "groups" subpatterns so that operators (such as +, *, {1,2}, and?) can be used on the enclosed characters as a group, and second it defines a back-reference. In the RewriteCond in your case, no operators are needed, and it appears that the back-reference to the subdomain name is not needed either, unless it is your intent to redirect requests for *any* page in a subdomain to the *index* page of a subdirectory in your main domain which is named identically to that subdomain (which is what your posted code does). An example would be widget.example.com/blue.html --> www.example.com/widget/ (note that "blue.html" is dropped).

Jim

scottrmay

11:38 pm on Dec 30, 2003 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for the reply. It is appreciated. I changed the script to read

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

I put this in an .htaccess file in the main directory. It did not work. I was not redirected to the main home page when I tried to access a subdomain. Instead I was directed to the "internet Optimizer" DNS error page.

Specifically, I want all subdomains pages to be redirected to the main domain home page. I only have one page that I am ever going to use on this web site. And I now want any link that comes to any part of this web site to be 301 directed to my home page.

For instance
http://subdomain1.example.com I want redirected to http://www.example.com

http://subdomain1.example.com/widgets.html I want redirected to http://www.example.com

http://subdomain2.example.com I want redirected to http://www.example.com

Once again, thanks for your quick reply. I have read through all material written about redirects and subdomains in webmasterworld and I haven't seen anything written about this particular circumstance. What do you think? Is it doable?

If I have to list all of the pages individually in the script I will. However there are a lot of them.

Peace,
Scott

[edited by: jdMorgan at 12:34 am (utc) on Dec. 31, 2003]
[edit reason] Examplified and de-linked [/edit]

fwwebs

12:27 am on Dec 31, 2003 (gmt 0)

10+ Year Member



What type of DNS records do you have? Unless you have a wildcard A record or an A record for the subdomain it will never forward to your IP.

I'm new to this forum, so please excuse me if I jumped into the middle of something thats already been addressed.

jdMorgan

12:28 am on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Scott,

> Instead I was directed to the "internet Optimizer" DNS error page.

Huh? This sounds like a DNS issue, not a mod_rewrite problem!

To redirect to your homepage, simply remove the parentheses and the "$1" from the RewriteRule I posted.

Back to first principals: If you take the htaccess code out and request one of your subdomains, where does that request 'land' - where does it take you, to a subdirectory of your main account directory, to another server, or where? The answer to this determines whether and where you need to install (an) htaccess file(s), and whether you need to make changes to your DNS zone file.

In order for htaccess to work, it must be present in the directory where a request for each subdoamin 'lands'. You can either install multiple htaccess files (one in each subdomain if they are hosted/located in different directories/servers, or you can change your DNS to point all subdomains to the same server, change your server config (if necessary) to point all subdomain requests to the same directory, and then apply htaccess to redirect all the requests to the same URL, "standardizing" your domain name.

The code modified as noted above will work, as long as you have Options +FollowSymLinks and RewriteEngine on ahead of it in your htaccess file. But there are multiple levels of control here and it sounds like we're only looking at one level.

Jim

jdMorgan

12:37 am on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



fwwebs,

Welcome to WebmasterWorld [webmasterworld.com]!

"...already been addressed" -- No, you beat my post by a minute! :)

Your contributions are most welcome.

Jim