Forum Moderators: phranque

Message Too Old, No Replies

Another 301 subdomain to domain question

         

kenj

4:31 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



We tested a virtual (points to the blog directory) subdomain with our blog for a day and changed our mind. Now we have pages out there with blog.example.com (was example.com/blog/ and was changed to blog.example.com and then changed back to example.com/blog/).

We have tried several rewrite variations in .htaccess like the following to redirect blog.example to example/blog/
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteRule (.*) http://www.example.com/blog/$1 [R=301,L]

blog.example redirects to example/ with or without the above rewrite.

We also have the following in the .htaccess file to resolve the non-www to www and /index.htm to / placed after the above.
# index.htm to /
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.htm\ HTTP/
RewriteRule ^(.*)index\.htm$ /$1 [R=301,L]
# non www to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Another side affect is the spiders that picked up on the blog.example are trying to do a robots.txt access for blog.example/robots.txt which times out and returns a 500 error code.

Here is what we want to do.
example.com to www.example.com
/index.htm to /
blog.example.com/ (all requests) to example.com/blog/ (i.e. blog.example.com/catgory/ to example.com/blog/category)

Any help will be most appreciated.

[edited by: jdMorgan at 5:25 pm (utc) on Nov. 29, 2007]
[edit reason] example.com [/edit]

jdMorgan

5:29 pm on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With the exception of a potential (but rare) infinite loop if you receive requests from an HTTP/1.0 client and some redundancy/efficiency problems, I don't see any problems. I'd suggest a clean-up as follows.

Options +FollowSymLinks
RewriteEngine on
#
# Redirect blog.example.com to www.example.com/blog
RewriteCond %{HTTP_HOST} !^blog\.example\.com
RewriteCond %{HTTP_HOST} .
RewriteRule (.*) http://www.example.com/blog/$1 [R=301,L]
#
# Redirect client requests for index.htm to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]
#
# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

blog.sitename redirects to sitename/ with or without the above rewrite.

Unless you've got some other code in your configuration that is interfering with your test results, this code should work as you require, but you certainly should not see the blog.example.com --> www.example.com/blog redirect if the first rule isn't present -- Which leads me to recommend that you make sure you are completely flushing your browser cache before testing any new configuration code on your server. If you don't, your browser may simply serve the previously-cached response, and won't send any request to your server. In that case, no server-side code can have any effect on the results.

Jim

kenj

6:34 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



Jim,
Thanks for helping. I struggle heavily with these things. Good news and bad news.

That set up redirects blog.sitename.com to sitename.com/blog/ however it also redirects www.sitename.com and sitename.com to sitename.com/blog/

Had to comment out the blog.sitename quickly so couldn't do more tests.

The other items in .htaccess are the errordocument entries which are first in the list and SetEnvIfNoCase set up to ban bad guys and specific file/directory redirects which follow the Rewrites we are looking at.

All checks done with cache cleared (good point).

The virtual domain set up for blog.sitename is still active. Does that matter?

Ken

kenj

11:09 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



After further research, I have the following questions.

# Redirect blog.example.com to www.example.com/blog
RewriteCond %{HTTP_HOST}!^blog\.example\.com
Does the! mean to negate everything in the test pattern?

RewriteCond %{HTTP_HOST} .
Does the . mean do the rewrite if 1 or more characters exist?

RewriteRule (.*) http://www.example.com/blog/$1 [R=301,L]
If the above assumptions are true, all scenarios are rewritten to www.example.com/blog/

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I am assuming that the ^ in the RewriteCond means there is no text in front of example.com therefore this condition would fail.

Removing the! in
RewriteCond %{HTTP_HOST}!^blog\.example\.com
appears to result in a false response and the rewrite is done as example.com/ either in this step or the later one.

Please let me know which assumptions are wrong so we can dig deeper.

Thanks,
Ken

jdMorgan

12:03 am on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



# Redirect blog.example.com to www.example.com/blog
RewriteCond %{HTTP_HOST} !^blog\.example\.com
Does the "!" mean to negate everything in the test pattern?

Yes, "!" is the negation operator. So the meaning is "If HTTP_HOST is NOT 'blog.example.com'"

RewriteCond %{HTTP_HOST} .
Does the . mean do the rewrite if 1 or more characters exist?

Yes, if the other RewriteConds are true as well. HTTP/1.0 clients will not send a "Host" header, and therefore, HTTP_HOST will be blank. Since you are using a negative-match pattern in the first RewriteCond, a blank hostname will match it. Therefore, it is a good idea to test for the blank-hostname condition to prevent an infinite redirection loop if an HTTP/1.0 host makes a request. You can also read this pattern as meaning "not blank," but it is shorter than the explicit "!^$" pattern of the same meaning.

RewriteRule (.*) http://www.example.com/blog/$1 [R=301,L]
If the above assumptions are true, all scenarios are rewritten to www.example.com/blog/

Yes, if both are true.

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I am assuming that the ^ in the RewriteCond means there is no text in front of example.com therefore this condition would fail.

It would fail if the requested hostname was already "www.example.com", preventing an infinite redirection loop.
However, it would match if "example.com" was requested, resulting in a redirect to "www.example.com", as intended.

Removing the "!" in
RewriteCond %{HTTP_HOST} !^blog\.example\.com
appears to result in a false response and the rewrite is done as example.com/ either in this step or the later one.

Yes, this is an example of a 'stacked' or 'chained' redirect. Avoid these cases where two redirects are invoked; Search engines will easily follow one redirect, and assign the ranking of the original URL to the new URL. They do not reliably do this if there are two or more redirects in a row.

Jim

[edited by: jdMorgan at 12:04 am (utc) on Nov. 30, 2007]

kenj

6:39 am on Nov 30, 2007 (gmt 0)

10+ Year Member



Problem resolved.

The blog redirect has to be in the blog folder. When requesting a subdomain.example.com/ Apache apparently transfers control to the folder for htaccess. I spotted this because the header information returned showed "X-Pingback: www.example.com/blog/xmlrpc.php" on the redirect to www.example.com/ The only place that could come from was wordpress. I hate to think how many more hours there would have been if it had been a regular folder.

The blog redirect works correctly before and after deleting the subdomain.

Thanks for the help Jim. It is scary but I am slowly beginning to understand how this stuff works.

edited to remove link