Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirects for subdomains

         

jimmybeige2

1:33 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



Hi there,
I am doing some htaccess redirects for some old existing subdomains. They are not in any particular format and have decent google rankings as the moment. I have created new pages for these and want to redirect the old subdomains to the new pages. Now I have not deleted the old subdomains and the folders still exist on the server and I would like to keep these without deleting them. I am using the following code:

RewriteCond %{HTTP_HOST} myoldsubdomain.mydomain.com$ [NC] [OR]
RewriteCond %{HTTP_HOST} www.myoldsubdomain.mydomain.com$ [NC]
RewriteRule (.*)$ [anothersubdomain.mydomain.com...] [R=301,L]

Is this a proper 301 redirect for a subdomain?

The problem I am having is that this does not work for my existing subdomains. It works fine if this subdomain doesn't already exist, but I would like to not have to delete the old folders on my server.

Is there a way of doing this or will I have to delete all my old subdomains before this code will work?

Thanks in advance for any help!

jdMorgan

2:30 pm on Jul 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first line of your code is invalid, the second is redundant, and both contain unescaped literal periods. I'd suggest:

RewriteCond %{HTTP_HOST} ^([^.]+\.)*myoldsubdomain\.mydomain\.com [NC]
RewriteRule ^ http://anothersubdomain.mydomain.com/page.htm [R=301,L]

Note that just like your original code, this code 'drops' the requested 'page' part of the URL. Is that what you wanted? It's not a very good idea to redirect all URL-paths to a single page. For example, what if you get a request for an image or the robots.txt file on this old subdomain? Neither should really be redirected to an HTML page.

This code does not 'care' if the old subdomain folders exist or not. It is possible that some other module or function on the server is interfering with its operation. If you are not using their features, then disable content-negotiation (MultiViews) and (if using Apache 2.x) disable AcceptPathInfo:


Options -MultiViews
AcceptPathInfo off

Otherwise, look for other mod_alias and/or mod_rewrite directives that may be interacting with your subdomain redirect rewriterules.

Jim

jimmybeige2

10:18 am on Jul 15, 2009 (gmt 0)

10+ Year Member



Thanks a lot for that Jim.

I used the code you suggested above. Problem still exists though but I think I can just delete all my old existing subdomains so that should solve the problem. I just didn't really want to do that.

The problem I have now is that these are rewrites for old subdomains, and there are hundreds of them. They were there before I started working on this site and I have been asked to redirect them all to new pages. As I mentioned above these are in no particular format and I have been writing individual rewrites for all of these but by htaccess file is getting quite big and is visibly slowing down the site.

Is there a limit to the size my htaccess file should be and is there any suggestion for reducing the size for what I am trying to do?
Many thanks.

jdMorgan

2:51 pm on Jul 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no prescribed maximum size for a .htaccess file. Really the only measure is what you stated above: It's too big when you can notice a slow-down in performance. Any other method gets too complex, since it depends on all aspects of your site: Traffic, available bandwidth, caching implementation, CPU speed, memory size and speed, disk speed, the number of other sites on this server -- everything.

This may come down to simple best-practice programming technique: Optimize the code for best performance in the 'normal' case. For example, if you have 100 subdomains, then skip processing unless the requested hostname *is not* the normal one:


[i]RewriteCond %{HTTP_HOST} !^the_correct_domain\.com[/i]
RewriteCond %{HTTP_HOST} ^([^.]+\.)*myoldsubdomain\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+\.)*anotheroldsubdomain\.example\.com [NC,OR]
... hundreds more RewriteConds
RewriteCond %{HTTP_HOST} ^([^.]+\.)*myoldsubdomain199\.example\.com [NC]
RewriteRule ^ http://anothersubdomain.example.com/page.htm [R=301,L]

-or-

# Skip next 200 rules if requested hostname is already correct
RewriteCond %{HTTP_HOST} ^the_correct_domain\.com
RewriteRule ^ - [S=200]
#
RewriteCond %{HTTP_HOST} ^([^.]+\.)*myoldsubdomain1\.example\.com [NC]
RewriteRule ^ http://anothersubdomain1.example.com/page.htm [R=301,L]
#
# ... 198 more redirect rules
#
RewriteCond %{HTTP_HOST} ^([^.]+\.)*myoldsubdomain200\.example\.com [NC]
RewriteRule ^ http://anothersubdomain200.example.com/page.htm [R=301,L]

Jim