Forum Moderators: phranque
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]
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.
Jim
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
# 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
# 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.
Jim
[edited by: jdMorgan at 12:04 am (utc) on Nov. 30, 2007]
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