Forum Moderators: phranque
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!
RewriteCond %{HTTP_HOST} ^([^.]+\.)*myoldsubdomain\.mydomain\.com [NC]
RewriteRule ^ http://anothersubdomain.mydomain.com/page.htm [R=301,L]
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
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.
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]
# 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]